5.动态SQL

作者: 落叶飞逝的恋 | 来源:发表于2017-08-27 20:57 被阅读105次

    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)
    

    区别就再在于参数上面。

    相关文章

      网友评论

        本文标题:5.动态SQL

        本文链接:https://www.haomeiwen.com/subject/rjuidxtx.html