MyBatis第八步、动态SQL语句

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>
















































  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值