动态SQL
动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。
使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。
如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器,你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中,需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式,MyBatis 3 替换了之前的大部分元素,大大精简了元素种类,现在要学习的元素种类比原来的一半还要少。
- if
- choose(when,otherwise)
- trim(where,set)
- foreach
if
- 正确的使用场景
<select id="findStudent">
select
*
from
student
<if test="name != null">
where name = #{name}
</if>
</select>
- 错误的使用场景
下面在使用多个 if 的时候,新手易出现的错误使用。
①第一种错误使用,在参数 name 为空时
语句就变成 where and id = #{id}
或者在两个参数都为空时
语句就变成单一个 where 在那里了,这样子都是抛异常的。
②第二种错误使用,在参数 id 为空时,语句就变成 select * from student and id = #{id}
这样也是会抛异常的。
<!-- 第一种错误的场景 -->
<select id="findStudent">
select
*
from
student
where
<if test="name != null">
name = #{name}
</if>
<if test="id != null">
and id = #{id}
</if>
</select>
<!-- 第二种错误的场景 -->
<select id="findStudent">
select
*
from
student
<if test="name != null">
where name = #{name}
</if>
<if test="id != null">
and id = #{id}
</if>
</select>
改善后
<select id="findStudent">
select
*
from
student
where true
<if test="name != null">
and name = #{name}
</if>
<if test="id != null">
and id = #{id}
</if>
</select>
choose(when,otherwise)
- choose(when,otherwise)
就类似于 if else 的感觉,也类似于 switch 的感觉
otherwise
就对于最后的 else 和 default
<!-- 对应的年龄查询对应年级学生 -->
<select id="findStudent">
select
id
,name
from
student
<choose>
<when test="age >= 7 and age <= 8">
where grade in (1,2)
</when>
<when test="age >= 9 and age <= 10">
where grade in (3,4)
</when>
<when test="age >= 11 and age <= 12">
where grade in (5,6)
</when>
<otherwise>
where false
</otherwise>
</choose>
</select>
trim(where,set)
- trim(where,set) 这一段引用官方文档的例子
<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 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。
如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
prefixOverrides 属性会忽略通过管道符分隔的文本序列(注意此例中的空格是必要的)。上述例子会移除所有 prefixOverrides 属性中指定的内容,并且插入 prefix 属性中指定的内容。
用于动态更新语句的类似解决方案叫做 set。set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。比如:
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
这个例子中,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。
来看看与 set 元素等价的自定义 trim 元素吧:
<trim prefix="SET" suffixOverrides=",">
...
</trim>
注意,我们覆盖了后缀值设置,并且自定义了前缀值。
foreach
<select id="findStudent">
select
id
,name
from
student
where id in
<foreach collection="list" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符,看它多智能!
提示
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号
,item 的值是本次迭代获取到的元素
。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键
,item 是值
。