MyBatis学习记录05——动态SQL

###0.前言
《Java EE互联网轻量级框架整合开发——SSM框架(Spring MVC+Spring+MyBatis)和Redis实现》

本文主要记录Mapper中的动态SQL语句的使用示例

动态SQL关键字:

  • if:条件判断(一般与test连用)
  • test:判断条件的真假
  • chose(when,otherwise):多条件分支,类似Java中switch语句。
  • foreach:循环语句
  • where:只会在至少有一个子元素的条件返回SQL子句的情况下才去插入“WHERE”子句。若语句的开头为“AND”或“OR”,where 元素也会将它们去除。
  • set:动态设置字段到update语句中,会动态移除语句结尾的多余的”,”
  • trim:移除prefixOverrides或者suffixOverrides属性中指定的内容,并且插入 prefix 属性中指定的内容
  • bind:从 OGNL 表达式中创建一个变量并将其绑定到上下文

###1.if和test
只要条件成立,子语句拼接到SQL中

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

上述示例的含义:如果参数title不为空,对列名title进行模糊查询,如果参数author不为空,则添加对列author_name的模糊查询

###2.chose(when,otherwise) 多条件查询
类似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.foreach 循环语句
对一个集合进行遍历,通常是在构建 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>

参数说明:

  • item:循环中的当前对象
  • index:循环中的当前索引(下标值)
  • collection:传入的集合,支持数组、List、Set、Map等。(使用Map时,index 是键,item 是值)
  • open:拼接字符串的开头
  • close:拼接字符串的结尾
  • separator:字符串的分隔符

上述示例拼接结果:

SELECT *
     FROM POST P
     WHERE ID in (item1,item2,...itemN)

###4.where
根据子语句的成立情况,在SQL语句中插入where子语句。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where>     <!--其中只要有一个if成立,为SQL语句添加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>

###5.set
动态设置字段到update语句中,会动态移除语句结尾的多余的”,”

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

###6.trim
移除prefixOverrides或者suffixOverrides属性中指定的内容,并且插入 prefix 属性中指定的内容

  • prefixOverrides:移除前缀
  • suffixOverrides:移除后缀

示例代码:

//示例1
<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <trim prefix="where" prefixOverrides="AND">     <!--其中只要有一个if成立,为SQL语句添加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>
  </trim>
</select>

//示例2
<update id="updateAuthorIfNecessary">
  update Author
    <trim prefix="SET" suffixOverrides=",">
      <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>
    </trim>
  where id=#{id}
</update>

where与set变为trim语句

  1. where => trim

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

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

###7. bind
从 OGNL 表达式中创建一个变量并将其绑定到上下文

<select id="selectBlogsLike" resultType="RoleBean">
      <bind name="pattern_roleName" value="'%' + roleName + '%'" />
    <bind name="pattern_note" value="'%' + note + '%'" />
  SELECT * FROM t_role
  WHERE role_name LIKE #{pattern_roleName}
  AND note LIKE #{pattern_note}  
</select>

END

– Nowy

– 2018.12.08

分享到