zoukankan      html  css  js  c++  java
  • 深入浅出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语句的过程。

  • 相关阅读:
    形态学操作
    形态学腐蚀膨胀操作
    图像模糊操作
    OpenCV像素操作和图形绘制
    c++中char类型的取值范围
    二叉树基本操作
    剑指27 二叉树的镜像
    剑指26 树的子结构
    剑指24: 反转链表
    剑指22 链表倒数第k个节点
  • 原文地址:https://www.cnblogs.com/mtime2004/p/9897409.html
Copyright © 2011-2022 走看看