MyBatis 动态 SQL MyBatis Dynamic SQL

动态 SQL 是 MyBatis 的强大特性之一。 使用动态 SQL 并非一件易事,MyBatis 显著地提升了这一特性的易用性。

1 if

使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。比如:

<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>

2 choose (when, otherwise)

有时候,我们不想使用所有的条件,而只是想从多个条件中选择一个使用。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

<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>

3 trim (where, set)

现在回到之前的 “if” 示例,这次我们将 “state = ‘ACTIVE’” 设置成动态条件,看看会发生什么。

<select id="findActiveBlogLike"
     resultType="Blog">
  select B.id, B.title, A.authorid, A.username 
from Blog B left outer join Author A on B.author_id = A.authorid
WHERE
<if test="state != null">
    B.state = #{state}
  </if>
  <if test="title != null">
    AND B.title like #{title}
  </if>
  <if test="author != null and author.username != null">
    AND A.username like #{author.username }
  </if>
</select>

如果没有匹配的条件会怎么样?最终这条 SQL 会变成这样:

SELECT * FROM BLOG WHERE

这会导致查询失败。 如果匹配的只是第二个条件又会怎样?这条 SQL 会是这样:

SELECT * FROM BLOG WHERE AND title like ‘someTitle’ 

这个查询也会失败。

MyBatis 有一个简单且适合大多数场景的解决办法。 而这,只需要一处简单的改动:

<select id="findActiveBlogLike"
     resultType="Blog">
  select B.id, B.title, A.authorid, A.username 
from Blog B left outer join Author A on B.author_id = A.authorid
  <where>
    <if test="state != null">
         B.state = #{state}
    </if>
    <if test="title != null">
        AND B.title like #{title}
    </if>
    <if test="author != null and author.username != null">
        AND A.username like #{author.username }
    </if>
  </where>
</select>

如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>

用于动态更新语句的类似解决方案叫做 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 元素等价的自定义 trim 元素吧:

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

属性

描述

prefix

在trim标签内SQL语句加上前缀

suffix

在trim标签内SQL语句加上后缀

prefixOverrides

指定去除多余的前缀内容

suffixOverrides

指定去除多余的后缀内容

4 foreach

动态 SQL 的另一个常见使用场景是对集合进行遍历。比如:

<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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章