mybatis动态sql

动态SQL

1.简介

动态sql是mybatis的一大强大功能,能帮助我们解决sql拼接的困难,动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。

2.if

动态 SQL 通常要做的事情是根据条件拼接sql,下边如果title和author都不为空的话,拼接的sql语句就会是
SELECT * FROM BLOG WHERE state = ‘ACTIVE’ AND title like #{title} AND author_name like {author.name}
如果都不符合条件,则会变成:SELECT * FROM BLOG WHERE state = ‘ACTIVE’

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

接着来
如果我把state也放在判断里边

    <select id="findActiveBlogLike"
         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 author_name like #{author.name}
      </if>
    </select>

仔细一想,如果没有一个符合条件,拼接的sql就会是

     SELECT * FROM BLOG WHERE 

如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:

    SELECT * FROM BLOG WHERE  AND title like ‘someTitle’

这显然都是错误的sql语句,应该怎么解决呢?
MyBatis 有一个简单的处理,就是where标签

    <select id="findActiveBlogLike"
         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 author_name like #{author.name}
        </if>
      </where>
    </select>

where标签只会在至少一个条件符合的情况下,才会插入’ where ',并且语句的开头为“AND”或“OR” ,还可以去除这多余的and和or
接着
如果我们有时候这样写呢

     <select id="findActiveBlogLike"
             resultType="Blog">
          SELECT * FROM BLOG 
          <where> 
            <if test="state != null">
                 state = #{state} AND
            </if> 
            <if test="title != null">
                title like #{title} AND
            </if>
            <if test="author != null and author.name != null">
                 author_name like #{author.name} 
            </if>
          </where>
        </select>

这样 where 标签就没作用了,这时候需要用到 trim 标签,where 标签只能解决前面多出的and 或者 or ,trim标签可以解决后面多出的and 或者 or
trim标签中有几个属性,
prefix :为 trim 标签体内拼接完的sql加一个前缀。
prefixoverride:前缀覆盖,覆盖trim标签体内拼接完成的sql前的字符
suffix:为 trim 标签体内拼接完的sql加一个后缀。
suffixoverride:后缀覆盖,覆盖trim标签体内拼接完成的sql后的字符

    <select id="findActiveBlogLike"
                 resultType="Blog">
              SELECT * FROM BLOG 
              <trim prefix = “where”  suffixoverride=“AND”> 
                <if test="state != null">
                     state = #{state} AND
                </if> 
                <if test="title != null">
                    title like #{title} AND
                </if>
                <if test="author != null and author.name != null">
                     author_name like #{author.name} 
                </if>
              </trim>
            </select>

3.choose, when, otherwise

上面是根据条件拼接,这个就是类似switch-case语句,根据条件选择sql,然后在拼接,只会拼接符合条件的那一个

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

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

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

6.blind

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

    <select id="selectBlogsLike" resultType="Blog">
      <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
      SELECT * FROM BLOG
      WHERE title LIKE #{pattern}
    </select>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值