5.动态SQL

MyBatis提供了对SQL语句动态组装能力,可以大大减少我们编写代码的工作量。

1.概述

MyBatis的动态SQL的包含以下元素

  • 1.if

判断语句,单条件分支判断

  • 2.choose(when\otherwise)

相当于Java中的case when语句,多条件分支判断

  • 3.trim(where\set)

辅助元素,用于处理一些SQL拼装问题

  • 4.foreach

循环语句,在in语句等列举条件常用

2.if元素

if元素是我们最常见的判断语句,相当于java中得if语句。常常与test属性联合使用。

<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">
    select * from blog
    where 1=1
    <if test="id>0">
        and blog_id=#{id}
    </if>
    <if test="title!=null and title!=''">
        and blog_title like concat('%',#{title},'%')
    </if>
</select>

3.choose、when、otherwise元素

有时候面临的更多的是类似java中的switch...case...default场景。这时候MyBatis就用choose...when...otherwise。

将上面的改造成这样的模式

<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">
    select * from blog
    where 1=1
    <choose>
        <when test="id>0">
            and blog_id=#{id}
        </when>
        <when test="title!=null and title!=''">
            and blog_title like concat('%',#{title},'%')
        </when>
        <otherwise>

        </otherwise>
    </choose>
</select>

4.trim、where、set元素

上面的例子我们使用了where 1=1的条件。这样的语句显示的很奇怪。但是去掉又会报错。这时候,我们可以使用where元素了。

<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">
    select * from blog
    <where>
    <if test="id>0">
        and blog_id=#{id}
    </if>
    <if test="title!=null and title!=''">
        and blog_title like concat('%',#{title},'%')
    </if>
    </where>
</select>

上面使用了where元素标签,同样我们可以使用trim元素标签来达到同样效果

<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">
    select * from blog
    <trim prefix="WHERE 1=1 and" prefixOverrides="and">
        <if test="id>0">
            and blog_id=#{id}
        </if>
        <if test="title!=null and title!=''">
            and blog_title like concat('%',#{title},'%')
        </if>
    </trim>
</select>

trim元素里面的prefix标签内字符用来替换prefixOverriders的标签字符。上面我们是为了测试效果才写成这样的。

<update id="update" parameterType="blog">
    update blog
    <set>
        <if test="title!=null and title!=''">
            blog_title=#{title},
        </if>
        <if test="authorId>0">
            blog_author_id=#{authorId},
        </if>
    </set>
    where blog_id=#{id}
</update>

5.foreach元素

批量更新的时候或者查询in的时候,传入的时集合对象

List<Blog> selectBlogByObject(@Param("ids") List<Integer> ids);
<select id="selectBlogByObject" resultMap="blogResult">
    select * from blog where blog_id IN
    <foreach collection="ids" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

或者这样写

List<Blog> selectBlogByObject(List<Integer> ids);
<select id="selectBlogByObject" resultMap="blogResult">
    select * from blog where blog_id IN
    <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

5.1 foreach属性

  • 1.collection

collection配置的时传递进来的参数名称。例如上面通过@Params传递进来的参数名称是ids。或者可以不使用@Params。那么collection就用list。

  • 2.item

item配置的是循环中当前的元素

  • 3.index

配置的是当前元素在集合的位置下标

  • 4.open \close

配置的是以什么符号将这些集合元素包装起来

  • 5.separator

是各个元素的间隔符

6.test的属性

test的属性用于条件判断的语句中。相当于判断真假。

<if test="authorId>0">
    blog_author_id=#{authorId},
</if>

上面语句的含义是如果是authorId大于0,那么就执行blog_author_id=#{authorId}。

7.bind元素

bind元素是通过OGNL表达式去自定义一个上下文变量。更方便我们使用。

比如模糊查询,mysql数据库是用%和参数相连。但是oracle数据库是用连接符||。但是有了bind元素,我们可以不用关注数据库语法的差异。

<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">

    <bind name="title" value="'%'+ title + '%'"/>
    select * from blog
    <trim prefix="WHERE 1=1 and" prefixOverrides="and">
        <if test="id>0">
            and blog_id=#{id}
        </if>
      
        <if test="title!=null and title!=''">
            and blog_title like #{title}
        </if>
    </trim>
</select>
Created connection 843467284.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32464a14]
==>  Preparing: select * from blog WHERE 1=1 and blog_id=? and blog_title like ? 
==> Parameters: 2(Long), %萧鼎的博客站点%(String)
  • 不使用bind元素
<select id="selectBlogByObject" parameterType="blog" resultMap="blogResult">
    select * from blog
    <where>
    <if test="id>0">
    and blog_id=#{id}
    </if>
    <if test="title!=null and title!=''">
    and blog_title like concat('%',#{title},'%')
    </if>
    </where>
</select>
Created connection 2007331442.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77a57272]
==>  Preparing: select * from blog WHERE blog_id=? and blog_title like concat('%',?,'%') 
==> Parameters: 2(Long), 萧鼎的博客站点(String)

区别就再在于参数上面。

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

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,762评论 0 4
  • 写在前面:十分感谢《深入浅出Mybatis技术原理与实战》这本书,大多数部分是书上的话,希望自己能在后面的文章中多...
    AceCream佳阅读 452评论 0 2
  • 1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...
    七寸知架构阅读 18,705评论 2 58
  • MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据...
    jackcooper阅读 499评论 0 1
  • 文|翌汐 01 “小苏,今年的工作总结和明年计划抓紧交初稿。” “小苏,项目统计报表做好了吗?” “小苏,把这个复...
    翌汐阅读 325评论 0 4