mybatis中静态sql语句有时不足以知足用户的需求,所以其提供了动态sql标签。java
if标签经过条件测试,动态插入sql片断,例如:sql
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">apache
<![CDATA[ select * from courses where tutor_id=#{tutorId} <if test="courseName!=null"> and name like #{courseName} </if> <if test="startDate!=null"> and start_date>=#{startDate} </if> <if test="endDate!=null"> and end_start<=#{endDate} </if> ]]>
</select>数组
<![CDATE[ ]]>保证之间的内容做为通常的字符处理,不作特殊处理。mybatis
choose标签用于选择第一个选择条件,例如: <select id="searchCourses" parameterType="hashmap" resultMap="CourseResult"> select * from courses <choose> <when test="searchBy=='Tutor'"> where tutor_id=#{tutorId} </when> <when test="searchBy=='CourseName'"> where name like #{CourseName} </when> <otherwise> where start_date=now(); </otherwise> </choose> </select>
有时,全部的查询条件可能都是可选的,可是其中至少有一个查询是须要的,可是如有多个查询条件都知足条件,这时就须要在查询条件添加and 或or。mybatis提供了where标签用于支持创建这种类型的sql语句,当第一个知足的条件前面有and 或 or 等链接词时,会自动删除链接词。例子以下所示: <select id="searchCourses" parameterType="hashmap" resultMap="CourseResult"> select * from courses <where> <if test="tutorId!=null"> tutor_id=#{tutorId} </if> <if test="courseName!=null"> and name like #{courseName} </if> <if test="startDate!=null"> and start_date=#{startDate} </if> <if test="endDate !=null"> and end_date=#{endDate} </if> </where> </select>
trim标签比where标签更加灵活,由于它能够在条件前面加上链接词或删除链接词 也能够在条件后面加上链接词或删除链接词;举例以下: <select id="searchCourses" parameterType="hashmap" resultMap="CourseResult"> select * from courses <trim prefix="where" prefixOverrides="and | or" > <if test="tutorId!=null"> tutor_id=#{tutorId} </if> <if test="courseName!=null"> and name like #{courseName} </if> <if test="startDate!=null"> and start_date=#{startDate} </if> <if test="endDate !=null"> and end_date=#{endDate} </if> </trim> </select>
foreach元素的属性主要有 item,index,collection,open,separator,close。 item表示集合中每个元素进行迭代时的别名. index指 定一个名字,用于表示在迭代过程当中,每次迭代到的位置. open表示该语句以什么开始. separator表示在每次进行迭代之间以什么符号做为分隔 符. close表示以什么结束. 举例以下: <select id="searchCourse" parameterType="hashMap" resultMap="Course"> select * from courses <if test="tutorIds!=null"> <where> <foreach item="tutorId" collection="tutorIds" > or tutor_id=#{tutorId} </foreah> </where> </if> </select> <select id="searchCourse" parameterType="hashMap" resultMap="Course"> select * from courses <if test="tutorIds!=null"> <where> tutor_id in <foreach item="tutorId" collection="tutorIds" open="(" separator="," close=")"> #{tutorId} </foreah > </where> </if> </select>
set标签相似于where标签,会在返回的条件前面插入set关键字,并移除最后一个条件的后面符号,举例以下所示: <update id="updateStudent" parameterType="Student"> update students <set> <if test="name!=null"> name=#{name},</if> <if test="email!=null">email=#{email},</if> <if test="phone!=null">phone=#{phone},</if> </set> </update> 若是三个条件都是ture,phone后面的逗号将被移除。
mybatis提供了提供了持久化的枚举类型。假设 Student表结构的gender列,使用varchar类型存储MALE或FEMALE,Student对象使用枚举类型标识gender. public enum Gender{ FEMALE,MALE } public class Student { .... private Gender gender; .... } <insert id="insert" parameterType="Student"> insert into student (id,name,gender) values(#{id},#{name},#{gender}) </insert> 当执行insert语句时,MALE或FEMALE会存储到gender列,若是想要存储的时枚举值而不是枚举名字,就须要配置类型处理器: <typeHander handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mybatis3.domain.Gender">
mybatis提供了对clob和blob的内检支持,将clob映射为java.lang.String,将blob映射为byte[]数组。所以用户能够像原始类型那样处理clob和blob类型。
mybatis提供了传递多个参数的内建功能,并经过#{param}语法引用参数。举例以下: List<Student> findAllStudentByNameEmail(String name,String email); <select id="findAllStudentByNameEmail" resultMap="Student"> select * from students where name=#{param1} and email=#{param2} </select>
要完成上述功能,须要使用sqlSession的selectMap方法; 举例以下: <select id="findAllStudent" resultMap="Student"> select * from students </select> Map<Integer,Student> studentMap=sqlSession.selectMap("com.mybatis3.mappers.StudentMapper.findAllStudent","studId");