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

来源:互联网 发布:宫内知美三级名字 编辑:程序博客网 时间:2024/06/14 21:56

一、前言

      前一篇博客中介绍了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=16SELECT * 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的对比”;

0 0
原创粉丝点击