四.Mybatis 动态sql语句

1、Mybatis动态SQL简介java

mybatis 的动态sql语句是基于OGNL表达式的,能够方便的在 sql 语句中实现某些逻辑.sql

整体说来mybatis 动态SQL 语句主要有如下几类:数组

  1. if 语句 (简单的条件判断)mybatis

  2. choose (when,otherwize) ,至关于java 语言中的 switch ,与 jstl 中的choose 很相似.app

  3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)框架

  4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,没必要担忧多余致使语法错误)ide

  5. set (主要用于更新时)spa

  6. foreach (在实现 mybatis in 语句查询时特别有用)code

下面分别介绍这几种处理方式对象

2、if

  这条语句的意思很是简单,若是你提供了title参数,那么就要知足title=#{title},

    一样若是你提供了Content和Owner的时候,它们也须要知足相应的条件,

    以后就是返回知足这些条件的全部Blog。

  这是很是有用的一个功能,以往咱们使用其余类型框架或者直接使用JDBC的时候,若是咱们要达到一样的选择效果的时候,

    咱们就须要拼SQL语句,这是极其麻烦的,比起来,上述的动态SQL就要简单多了

  <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">
    select * from t_blog where 1 = 1
     <if test="title != null">
          and title = #{title}
        </if>
        <if test="content != null">
            and content = #{content}
        </if>
        <if test="owner != null">
            and owner = #{owner}
        </if>
  </select>

3、choose(when,otherwise)

  when元素表示当when中的条件知足的时候就输出其中的内容,跟JAVA中的switch效果差很少的是按照条件的顺序,

    当when中有条件知足的时候,就会跳出choose,

    即全部的when和otherwise条件中,只有一个会输出,

    当全部的条件都不知足的时候就输出otherwise中的内容。

  因此上述语句的意思很是简单, 当title!=null的时候就输出and titlte = #{title},再也不往下判断条件,

                当title=null 且content!=null的时候就输出and content = #{content},

                当全部条件都不知足的时候就输出otherwise中的内容。

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
    select * from t_blog where 1 = 1 
      <choose>
          <when test="title != null">
               and title = #{title}
          </when>
          <when test="content != null">
               and content = #{content}
          </when>            
        <otherwise>
               and owner = "owner1"
          </otherwise>
      </choose>
</select>

4、trim

  trim元素的主要功能是能够在本身包含的内容前加上某些前缀,也能够在其后加上某些后缀,与之对应的属性是prefix和suffix;

    能够把包含内容的首部某些内容覆盖,即忽略,也能够把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;

    正由于trim有这样的功能,因此咱们也能够很是简单的利用trim来代替where元素的功能

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
    select * from t_blog 
        <trim prefix="where" prefixOverrides="and |or">
            <if test="title != null">
                title = #{title}
            </if>
            <if test="content != null">
                and content = #{content}
            </if>
            <if test="owner != null">
                or owner = #{owner}
            </if>
        </trim></select>

5、where

  where元素的做用是会在写入where元素的地方输出一个where,

    另一个好处是你不须要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,

    若是全部的条件都不知足那么MyBatis就会查出全部的记录,

    若是输出后是and 开头的,MyBatis会把第一个and忽略,固然若是是or开头的,MyBatis也会把它忽略;

    此外,在where元素中你不须要考虑空格的问题,MyBatis会智能的帮你加上。

  像上述例子中,若是title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},

      而不是select * from t_blog where and content = #{content},由于MyBatis会智能的把首个and 或 or 给忽略。

 <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
    select * from t_blog 
        <where>
            <if test="title != null">
                title = #{title}
            </if>
            <if test="content != null">
                and content = #{content}
            </if>
            <if test="owner != null">
                and owner = #{owner}
            </if>
        </where></select>

6、set

  set元素主要是用在更新操做的时候,它的主要功能和where元素实际上是差很少的,

    主要是在包含的语句前输出一个set,而后若是包含的语句是以逗号结束的话将会把该逗号忽略,

    若是set包含的内容为空的话则会出错。

  有了set元素咱们就能够动态的更新那些修改了的字段。

 <update id="dynamicSetTest" parameterType="Blog">
     update t_blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="content != null">
                content = #{content},
           </if>
            <if test="owner != null">
                owner = #{owner}
            </if>
        </set>       where id = #{id} </update>

 

7、foreach

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

  foreach元素的属性主要有item,index,collection,open,separator,close。

    item表示集合中每个元素进行迭代时的别名,

    index指定一个名字,用于表示在迭代过程当中,每次迭代到的位置,

    open表示该语句以什么开始,

    separator表示在每次进行迭代之间以什么符号做为分隔符,

    close表示以什么结束,

  在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,

    可是在不一样状况下,该属性的值是不同的,主要有一下3种状况:

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

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

       若是传入的参数是多个的时候,咱们就须要把它们封装成一个Map了,固然单参数也能够封装成map,

    实际上若是你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,

      因此这个时候collection属性值就是传入的List或array对象在本身封装的map里面的key

Foreach-List:

  对应的mapper里面的方法

public List<Blog> dynamicForeachTest(List<Integer> ids);
 <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>

Foreach-数组:

  对应的mapper方法

 public List<Blog> dynamicForeach2Test(int[] ids);  
<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>

另外批量新增数据也会用到foreach,用法相似,这里就再也不赘述了

相关文章
相关标签/搜索