【Mybatis】(七)动态SQL的元素

动态SQL简介

MyBatis的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。

有些时候,SQL语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。使用Oracle的序列、mySQL的函数生成Id。

这时我们可以使用动态SQL。利用动态SQL 这一特性可以彻底摆脱这种痛苦。通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中。动态 SQL 元素和使用 JSTL 或其他类似基于 XML的文本处理器相似。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。

Mybatis中的动态SQL元素

  • <if>:使用if实现简单的条件判断。
  • <where>:简化SQL语句中的where的条件判断。
  • <choose>( when、otherwise ):相当Java的 swith ,满足条件将会跳出。
  • <set>:解决动态更新语句。
  • <trim>:可以灵活的去除多余的关键字。
  • <foreach>:迭代一个集合,通常用于in条件。

一、if_where标签

1、<if> 标签 用于条件判断,test 表示条件表达式。

2、where标签(用于动态判断)

  • 给里面的内容动态添加 where 关键字前缀;否则反之。
  • 只会去除第一个多出来的 and 或者 or
<select id="findStuByBlurry" resultType="Student">
    select * from student
    <where>
        <if test="stuName != null and stuName != ''">
            and stuName like concat('%',#{stuName},'%')
        </if>
        <if test="stuAge != null and stuAge != ''">
            and stuAge = #{stuAge}
        </if>
        <if test="stuSex != null and stuSex != ''">
            and stuSex = #{stuSex}
        </if>
    </where>
</select>

二、set标签

标签:

  • 根据内容是否为空动态添加 set 关键字。
  • 标签内容内最后面多余的逗号。
<update id="updateStuOne" paramterType="Student">
    update student
    <set>
        <if test="stuName != null |stuName!=''">
            stuName = #{stuName},
        </if>
        <if test="stuAge != null |stuAge!=''">
            stuAge  = #{stuAge},
        </if>
        <if test="stuSex != null |stuSex!=''">
            stuSex = #{stuSex},
        </if>
    </set>
    where stuId = #{stuId}
</update>

三、trim标签

标签:表示整理,更加灵活自定义,达到set、where或者其他效果。

1、前缀属性
​ prefix :内容整体添加前缀;没内容则不添加。
​ prefixOverrides:表示除去prefix后面第一个多余的关键字。

2、后缀属性
​ suffix :内容整体添加后缀;没内容则不添加。
​ suffixOverrides:表示除去suffix前面,也就是最后一个的多余关键字。

3、多个关键字使用 and|or 表示,“|”后面不能添加空格,否则不能识别。

<select id="findStuByBlurry" resultType="Student">
    select * from student 
    <trim prefix="where" prefixOverrides="and|or" suffix="order by stuId desc" suffixOverrides="and|or">
        <if test="stuName != null and stuName != ''">
            or stuName like concat('%',#{stuName},'%')
        </if>
        <if test="stuAge != null and stuAge != ''">
            and stuAge = #{stuAge}
        </if>
        <if test="stuSex != null and stuSex != ''">
            and stuSex = #{stuSex}
        </if>
    </trim>
</select>

四、choose_when_otherwise标签

<!-- 相当于Java的 switch...case,哪个条件成立执行哪个,条件不成立执行oherwise-->
<select id="findStuByBlurry2" resultType="Student">
    select * from student where
    <choose>
        <when test="stuName != null and stuName != ''">
            stuName = #{stuName}
        </when>
        <when test="stuId != null and stuId != ''">
            stuId = #{stuId}
        </when>
        <otherwise>
            stuSex = #{stuSex}
        </otherwise>
    </choose>
</select>

五、foreach标签

foreach标签:

  • collection属性默认值:

    1.如果是数组,默认 collection = "array"

    2.如果是集合,默认 collection = "list"

  • 自定义collection属性值:在参数前添加 @Param(“name”) 注解,则 collection = "name"

  • 遍历 map 对象

    1.遍历map对象,添加注解,添加参数类型=map。
    collection = “注解值”

    2.遍历Map对象中的对象,比如Map中的List。
    ​ collection = “map的key名称”

<select id="findManyById" resultType="Student">
    select * from student where id in
    <foreach collection="params" open="(" close=")" separator="," item="item">
        #{item}
    </foreach>
</select>

foreach属性:

collection = 需要遍历的参数
index = 遍历的下标位置
open = 添加整体标签内容的前缀
close = 添加整体标签内容的后缀
separator = 内容之间的分隔符
item = 别名,取值通过,#{item值}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值