MyBatis 动态

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这个一个基于Mybatis的驱动框架,该框架专门用于处理自主研发的Bin SQL,以更加方便快捷的方式实现Myba...
    彬_708c阅读 692评论 0 0
  • MyBatis动态SQL语句 MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类...
    木易林1阅读 626评论 0 1
  • 一 动态sql 1.1 什么是动态sql? mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对...
    Eugene1024阅读 1,868评论 0 0
  • Mybatis 动态SQL,通过●if●choose (when, otherwise)●trim (where,...
    赵哥窟阅读 122评论 0 0
  • MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据...
    jackcooper阅读 498评论 0 1