您的位置:首页 > 数据库

MyBatis第八步、动态SQL语句

2016-12-28 10:45 369 查看
mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:

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

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

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

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

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

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

1. mybaits if 语句处理,示例如下:

<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>

2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似,示例如下:

【注意】条件中只有一个when会被执行。

<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>

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

<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>

4、where (主要是用来简化sql语句中where条件判断的,能智能的处理 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>

5、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>

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

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

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

3.    如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,

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

      所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key。

单参数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>

单参数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>

多参数封装成Map的类型

   <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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: