MyBatis源码学习笔记(九) 动态sql

动态sql

原文:http://www.cnblogs.com/dongying/p/4092662.html

传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误。Mybatis的动态SQL功能正是为了解决这种问题, 其通过 if, choose, when, otherwise, trim, where, set, foreach标签,可组合成非常灵活的SQL语句。

1、if

<select id="findUserById" resultType="UserEntity">
           select * from user where 
           <if test="id != null">
               id=#{id}
           </if>
            and deleteFlag=0;
</select>

上面的例子存在的问题:

如果传入的id 不为空, 那么才会SQL才拼接id = #{id}。
要是你传入的id为null, 那么你这最终的SQL语句不就成了 select * from user where and deleteFlag=0, 这语句有问题,所以这里要使用where标签

2、where

<select id="findUserById" resultType="UserEntity">
           select * from user 
           <where>
               <if test="id != null">
                   id=#{id}
               </if>
               and deleteFlag=0;
           </where>
 </select>

从表面上来看,就是多了个where标签而已, 不过实质上, mybatis是对它做了处理,当它遇到AND或者OR这些,它知道怎么处理。其实我们可以通过 trim 标签去自定义这种处理规则。

3、trim:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>

当WHERE后紧随AND或则OR的时候,就去除AND或者OR。

4、set:

<update id="updateUser" parameterType="UserEntity">
           update user set 
           <if test="name != null">
               name = #{name},
           </if> 
           <if test="password != null">
               password = #{password},
           </if> 
           <if test="age != null">
               age = #{age}
           </if> 
           <where>
               <if test="id != null">
                   id = #{id}
               </if>
               and deleteFlag = 0;
           </where>
</update>

这样写的问题:
只有name不为null, 那么这SQL不就成了 update set name = #{name}, where …….. ? 你那name后面那逗号会导致出错。所以,这里要使用set标签:

<update id="updateUser" parameterType="UserEntity">
           update user
        <set>
          <if test="name != null">name = #{name},</if> 
          <if test="password != null">password = #{password},</if> 
          <if test="age != null">age = #{age},</if> 
        </set>
           <where>
               <if test="id != null">
                   id = #{id}
               </if>
               and deleteFlag = 0;
           </where>
</update>

这个用trim 可表示为:

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

使用trim时,WHERE是使用的 prefixOverrides(前缀), SET是使用的 suffixOverrides (后缀)

5、foreach

mybatis中有foreach, 可通过它实现循环,循环的对象主要是java容器和数组。

<select id="selectUserIn" resultType="UserEntity">
  SELECT *
  FROM user u
  WHERE id in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

将一个 List 实例或者数组作为参数对象传给 MyBatis,当这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。同样, 当循环的对象为map的时候,index其实就是map的key。

6、choose

<select id="findUserLike"
     resultType="UserEntity">
  SELECT * FROM user 
  <choose>
    <when test="name != null">
      AND name like #{name}
    </when>
    <when test="password != null">
      AND password like #{password}
    </when>
    <otherwise>
      AND deleteFlag = 1
    </otherwise>
  </choose>
</select>

当name和password都不为null的时候, 那么选择二选一(前者优先), 如果都为null, 那么就选择 otherwise中的, 如果name和password只有一个不为null, 那么就选择不为null的那个。

猜你喜欢

转载自blog.csdn.net/zxzzxzzxz123/article/details/73198564
今日推荐