MyBatis理解与掌握(动态SQL)

MyBatis理解与掌握(动态SQL)

@(MyBatis)[Java, 框架, MyBatis]java

if

if 就是__简单的条件判断 __,利用if语句咱们能够实现某些简单的条件选择。
先来看以下一个例子:sql

<select id="selectUserByUserNameAndSex" resultType="com.george.pojo.User" parameterType="com.george.pojo.User">
    select * from user where
        <if test="userName != null">
           username=#{userName}
        </if>
         
        <if test="userName != null">
           and sex=#{sex}
        </if>
</select>

在JDBC中若是要实现条件查询,采用的是拼串的方式,并且sql语句每每和java代码混杂在一块儿,很是麻烦。数组

choose(when,otherwise)

choose元素的做用就至关于JAVA中的switch语句,基本上跟JSTL中的choose的做用和用法是同样的,一般都是与when和otherwise搭配的。看以下一个例子:框架

<select id="selectUserByChoose" resultType="com.george.pojo.User" parameterType="com.george.pojo.User">
      select * from user
      <where>
          <choose>
              <when test="id !='' and id != null">
                  id=#{id}
              </when>
              <when test="userName !='' and userName != null">
                  and username=#{userName}
              </when>
              <otherwise>
                  and sex=#{sex}
              </otherwise>
          </choose>
      </where>
  </select>

when元素表示当when中的 条件知足的时候就输出其中的内容 ,只有一个会输出 ,当title!=null的时候就输出and titlte = #{title},再也不往下判断条件,当title为空且content!=null的时候就输出and content = #{content},当全部条件都不知足的时候就输出otherwise中的内容。ide

where

where语句的做用主要是 简化SQL语句中where中的条件判断 ,先看一个例子,再解释一下where的好处。url

<select id="selectUserByUserNameAndSex" resultType="com.george.pojo.User" parameterType="com.george.pojo.User">
    select * from user
    <where>
        <if test="userName != null">
           username=#{userName}
        </if>
         
        <if test="userName != null">
           and sex=#{sex}
        </if>
    </where>
</select>

(1)会在写入where元素的地方输出一个where
(2)若是全部的条件都不知足那么MyBatis就会查出全部的记录
(3)若是输出后是and 开头的,MyBatis会把第一个and忽略,固然若是是or开头的,MyBatis也会把它忽略
(4)在where元素中你不须要考虑空格的问题,MyBatis会智能的帮你加上code

trim

<insert id="addBrand" parameterType="Brand">
    insert into bbs_brand
    <trim prefix="(" suffix=")">
     name,
     description,
     img_url,
     sort,
     is_display
    </trim>
    values
    <trim prefix="(" suffix=")">
     #{name},
     #{description},
     #{imgUrl},
     #{sort},
     #{isDisplay}
    </trim>
</insert>

trim元素的主要功能是能够在本身包含的内容前加上某些前缀,也能够在其后加上某些后缀,与之对应的属性是prefixsuffix.
能够把包含内容的首部某些内容覆盖,即忽略,也能够把尾部的某些内容覆盖,对应的属性是prefixOverridessuffixOverridesxml

set

set元素主要是用 在更新操做的时候在包含的语句前输出一个set 。
有了set元素咱们就能够动态的更新那些修改了的字段。下面是一段示例代码:对象

<update id="updateUserById" parameterType="com.george.pojo.User">
    update user u
        <set>
            <if test="userName != null and userName != ''">
                u.username = #{userName},
            </if>
            <if test="sex != null and sex != ''">
                u.sex = #{sex}
            </if>
        </set>
     
     where id=#{id}
</update>

(1)若是包含的语句是以逗号结束的话将会把该逗号忽略
(2)若是set包含的内容为空的话则会出错blog

foreach

foreach的主要用在 构建in条件中 ,它能够在SQL语句中进行迭代一个集合

foreach元素的属性主要有:

  • item表示集合中每个元素进行迭代时的别名
  • index指定一个名字,用于表示在迭代过程当中,每次迭代到的位置
  • open表示该语句以什么开始
  • separator表示在每次进行迭代之间以什么符号做为分隔符
  • close表示以什么结束
  • collection属性,该属性是必须指定的,可是在不一样状况下,该属性的值是不同的,主要有一下3种状况:

(1)果传入的是单参数且参数类型是一个List的时候,collection属性值为list

<select id="dynamicForeachTest" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")" >
    #{item}
</foreach>
</select>
public List<Blog> dynamicForeachTest(List<Integer> ids);

(2)若是传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

<select id="dynamicForeach2Test" resultType="Blog">
select * from t_blog where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")" >
    #{item}
</foreach>
</select>
public List<Blog> dynamicForeach2Test(int[] ids);

(3)若是传入的参数是多个的时候,咱们就须要把它们封装成一个Map了,因此这个时候collection属性值就是传入的List或array对象在本身封装的map里面的key

<select id="dynamicForeach3Test" resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")" >
    #{item}
</foreach>
</select>
public List<Blog> dynamicForeach3Test(Map<String, Object> params);

基于3.3.1版本验证的新特性
(4)foreach标签遍历的集合元素类型是Map.Entry类型时,index属性指定的变量表明对应的Map.Entry的key,item属性指定的变量表明对应的Map.Entry的value。此时若是对应的集合是Map.entrySet,则对应的collection属性用collection。foreach在进行遍历的时候若是传入的参数是List类型,则其collection属性的值能够是list或collection,但若是传入的参数是Set类型,则collection属性的值只能用collection。

<select id="dynamicForeach2Test" resultType="Blog">
select * from t_blog where id in
<!-- 遍历的对象是Map.Entity时,index表明对应的Key,item表明对应的value   -->
<foreach collection="collection" index="key" item="value" open="(" separator="," close=")" >
    #{key},#{value}
</foreach>
</select>
public List<Blog> dynamicForeachTest(Set<Map.Entry<Integer, Integer>> ids);

bind

bind标签,动态SQL中已经包含了这样一个新的标签,它的功能是在当前OGNL上下文中建立一个变量并绑定一个值。
有了它之后咱们之前的模糊查询就能够改为这个样子:

<select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String">
<!-- bind标签用于建立新的变量   -->
  <bind name="titleLike" value="'%' + _parameter + '%'" />
  SELECT * FROM t_blog
  WHERE title LIKE #{titleLike}
</select>
相关文章
相关标签/搜索