if
<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>
如果想可选地通过"title"和"author"两个条件搜索,首先,改变语句的名称让它更具实际意义;然后只要加入另一个条件即可。
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
choose, when, otherwise
MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。提供了"title"就按"title"查找,提供了"author"就按"author"查找,若两者都没有提供,就返回所有符合条件的BLOG
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
trim, where, set
MyBatis 有一个简单的处理,这在90%的情况下都会有用。
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
where 元素知道只有在一个以上的if条件有值的情况下才去插入"WHERE"子句。而且,若最后的内容是"AND"或"OR"开头的,where 元素也知道如何将他们去除。
如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<select id="selectEmps" resultType="Emp">
<!-- SELECT * FROM emp e, dept d WHERE e.deptno=d.deptno -->
select * from emp e
<!-- <if test="ename != null"> where e.ename like #{ename} </if> -->
<trim prefix="where" prefixOverrides="and|or">
<if test="ename != null">
and e.ename like #{ename}
</if>
<if test="sal != null">
and e.sal > #{sal}
</if>
<if test="empnoList != null">
and e.empno in
<foreach collection="empnoList" item="empno" open="(" close=")"
separator=", " index="a">
#{empno}
</foreach>
</if>
</trim>
</select>
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它带来的结果就是所有在 prefixOverrides 属性中指定的内容将被移除,并且插入 prefix 属性中指定的内容。
类似的用于动态更新语句的解决方案叫做 set。set 元素可以被用于动态包含需要更新的列,而舍去其他的。
<update id="updateEmp" parameterType="Emp">
update emp e
<set>
<if test="ename != null">
e.ename=#{ename},
</if>
<if test="job != null">
e.job=#{job},
</if>
<if test="mgr != null">
e.mgr=#{mgr},
</if>
<if test="hiredate != null">
e.hiredate=#{hiredate},
</if>
<if test="sal != null">
e.sal=#{sal},
</if>
<if test="comm != null">
e.comm=#{comm},
</if>
</set>
<where>
e.empno=#{empno}
</where>
</update>
这里,set 元素会动态前置 SET 关键字,同时也会消除无关的逗号,因为用了条件语句之后很可能就会在生成的赋值语句的后面留下这些逗号。
foreach
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。比如:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以"list"作为键,而数组实例的键将是"array"。
<select id="selectEmpsByList" resultType="Emp">
select e.*,sq_test.nextval from emp e
<trim prefix="where" prefixOverrides="and|or">
and e.empno in
<foreach collection="list" item="empno" open="(" close=")"
separator=", " index="a">
#{empno}
</foreach>
</trim>
</select>