MyBatis的动态SQL元素

版权声明:Unruly https://blog.csdn.net/weixin_41660948/article/details/84929901

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

标签

  1. 根据内容是否为空动态添加 set 关键字。
  2. 标签内容内最后面多余的逗号。
<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值

猜你喜欢

转载自blog.csdn.net/weixin_41660948/article/details/84929901