【Mybatis】深入浅出Mybatis(八)——动态SQL

一、前言

      前一篇博客中介绍了Mybatis的别名的使用,这个方法也是非常方便的。下面小编向大家介绍一下Mybatis的最精彩的亮点——动态SQL。通过mybatis提供的各种标签方法实现动态拼接sql。

二、if

      if标签可以起到判断的作用,用来判断我们所要查询的字段是否为空或者是‘’,可以让sql语句更加的灵活。提高了复用性。

      PS:注意要做不等于空字符串校验。

    <!-- 传递pojo综合查询用户信息 -->
    <select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        where 1=1 

        <if test="id!=null and id!=''">
        and id=#{id}
        </if>

        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
    </select>

      如果条件都成立的话,最终的sql语句就是:

select * from user where  id=#{id} and username like '%${username}%'

三、Where

      Mybatis提供了where,可以自动处理第一个and。

      比如在下面的例子中,如果id为不为空或者不为‘’,会自动的判断是否需要and,如果需要的话就补上,如果不需要,Mybatis就自动去掉and。

    <select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
        <if test="id!=null and id!=''">
        and id=#{id}
        </if>
        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
        </where>
    </select>

      如果条件都成立的话,最终的sql语句就是:

select * from user where  id=#{id} and username like '%${username}%'

四、foreach

      向sql传递数组或List,mybatis使用foreach解析

      在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下:

      两种方法:

SELECT * FROM USER WHERE id=1 OR id=10 OR id=16

SELECT * FROM USER WHERE id IN(1,10,16)

      使用 foreach遍历传入ids,他有下面的标签:

  • collection:指定输入 对象中集合属性

  • item:每个遍历生成对象中

  • open:开始遍历时拼接的串

  • close:结束遍历时拼接的串

  • separator:遍历的两个对象中需要拼接的串

      实践代码如下:

            <if test="ids!=null">
            <!-- 使用 foreach遍历传入ids
            collection:指定输入 对象中集合属性
            item:每个遍历生成对象中
            open:开始遍历时拼接的串
            close:结束遍历时拼接的串
            separator:遍历的两个对象中需要拼接的串
             -->
             <!-- 使用实现下边的sql拼接:
              AND (id=1 OR id=10 OR id=16) 
              -->
            <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
                <!-- 每个遍历需要拼接的串 -->
                id=#{user_id}
            </foreach>

            <!-- 实现  “ and id IN(1,10,16)”拼接 -->
            <!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
                每个遍历需要拼接的串
                #{user_id}
            </foreach> -->

            </if>

五、sql片段

      我们可以把上面实现的动态sql判断代码块抽取出来,组成一个sql片段。当其他的statement中需要,就可以引用sql片段了。这样sql代码的复用率就提高了,程序员开发就更加的方便了。

      Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:

<!-- 传递pojo综合查询用户信息 -->
    <select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
        <if test="id!=null and id!=''">
        and id=#{id}
        </if>
        <if test="username!=null and username!=''">
        and username like '%${username}%'
        </if>
        </where>
    </select>

      将where条件抽取出来:

<sql id="query_user_where">
    <if test="id!=null and id!=''">
        and id=#{id}
    </if>
    <if test="username!=null and username!=''">
        and username like '%${username}%'
    </if>
</sql>

      Sql片段的引用,使用include引用:

<select id="findUserList" parameterType="user" resultType="user">
        select * from user 
        <where>
        <include refid="query_user_where"/>
        </where>
    </select>

      注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
      

六、小结

      动态SQL就是复用sql语句的过程。

      要想体验到这个过程,就必须要多多的尝试一下,必须多多的锻炼,多用,才能把Mybatis的好处发挥到极致。

      下一篇博客向大家带来“深入浅出Mybatis(九)——Mybatis和hibernate的对比”;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值