MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。,MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。从而拼接成相应的sql语句。
1、if
在动态 SQL 中所做的最通用的事情是包含部分 where 字句的条件。比如:
- <select id="findActiveBlogWithTitleLike" parameterType="Blog"
- resultType="Blog">
- SELECT * FROM BLOG
- WHERE state = "ACTIVE"
- <if test="title ! null ">
- AND title like #{title}
- </if>
- </select>
- <select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}
- </if>
- </select>
有时我们不想应用所有的条件,相反我们想选择很多情况下的一种。 Java 中的 switch和语句相似,MyBatis 提供 choose 元素。我们使用上面的示例,但是现在我们来搜索当 title 提供时仅有 title 条件,当 author 提供时仅有 author 条件。如果二者都没提供,只返回 featured blogs(也许是由管理员策略地选择的结果列表,而不是返回大量没有意义的,随机的博客结果列表)。
- <select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}
- </when>
- <otherwise>
- AND featured = 1
- </otherwise>
- </choose>
- </select>
3、trim, where, set
前面的例子已经方便地处理了一个臭名昭著的动态 SQL 问题。要考虑我们回到“if”示例后会发生什么,但是这次我们将“ACTIVE = 1”也设置成动态的条件。
- <select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}
- </if>
- </select>
SELECT * FROM BLOG WHERE
这会导致查询失败。如果仅仅第二个条件匹配是什么样的?这条 SQL 结束时就会是这样:
SELECT * FROM BLOG WHERE AND title like „someTitle‟
这个查询也会失败。这个问题不能简单的用条件来解决,或许你会说可以这样写:
- <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">
- SELECT * FROM BLOG WHERE 1=1
- <if test="state ! null ">
- and state = #{state}
- </if>
- <if test="title ! null ">
- AND title like #{title}
- </if>
- <if test="author ! null and author.name ! =null">
- AND title like #{author.name}
- </if>
- </select>
MyBatis 有一个简单的处理,这在 90%的情况下都会有用。而在不能使用的地方,你可以自定义处理方式。加上一个简单的改变,所有事情都会顺利进行:
- <select id="findActiveBlogLike" parameterType="Blog" 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 title like #{author.name}
- </if>
- </where>
- </select>
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,例如:
- <select id="test" parameterType="map" resultType="dept">
- select * from scott.Dept
- <trim prefix="where" prefixOverrides="and | or" suffixOverrides=",">
- <if test="startId != null">
- <![CDATA[and deptno <=#{startId}]]>
- </if>
- <if test="endId != null">
- <![CDATA[or deptno >= #{endId},]]>
- </if>
- </trim>
- </select>
或者用于更新
- <update id="updateDept" parameterType="dept">
- update scott.dept
- <trim prefix="set" suffixOverrides=",">
- <choose>
- <when test="dname!=null">
- dname=#{dname},
- </when>
- <otherwise>
- dname='123',
- </otherwise>
- </choose>
- <if test="loc!=null">
- loc=#{loc},
- </if>
- </trim>
- where deptno=#{deptno}
- </update>
set 元素可以被用于动态包含更新的列,而不包含不需更新的。比如:
- <update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
- 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>
<trim prefix="SET" suffixOverrides=",">
...
</trim>
注意这种情况下我们覆盖一个后缀,而同时也附加前缀。
4、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>
注意:你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名称作为键。List 实例将会以“list”作为键,而数组实例将会以“array”作为键。