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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大强012

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值