Mybatis入门(七)SpringBoot整合Mybatis_3 动态SQL标签


一、Mybatis 动态SQL标签有哪些

  • <if>
  • <choose>,<when>,<otherwise>
  • <foreach>
  • <trim>,<where>,<set>

二、Mybatis 动态SQL标签的使用

1. <if> 标签的使用

1. 1 <if> 标签的作用

<if> 标签和我们Java中 分支结构 中的 if 单路分支一样,主要起到的是判断的作用,满足则执行
<if> 标签中的 SQL语句,不满足则不执行 <if> 标签中的SQL语句。

1.2 <if> 标签的常用场景

经常使用的场景:where子句中,新增操作中,修改操作中,查询操作

1.3 <if> 标签的属性

<if> 标签提供和一个 test 属性,这是一个必填项。

1.4 <if> 标签的使用案例

<if> 搭配 where子句

    <!-- 根据性别及班级号查询学生信息   -->
    <!--  select * from student where ssex = ? and cid = ?;  -->
    <select id="getStudentByCondition" resultType="com.cy.mybatis.pojo.entity.Student">
        select * from student
        where
        <if test="ssex != null"> ssex = #{ssex}</if>
        <if test="cid != null"> and c_id = #{cid}</if>
    </select>

这个语句提供的是一个可选的查询功能,如果传递的条件不为 null 则拼接 <if> 标签中的 SQL语句,如果为 null 则不拼接 <if> 标签中的 SQL语句

但是这个有些瑕疵(某些情况下会出现问题),我们之后会讲到。

2. <where> 标签的使用

1. 1 <where> 标签的作用

<where> 主要用于条件子句中,使用 <where> 标签 相较于 where 关键字好很多,避免很多错误。

1.2 <where>标签的常用场景

经常使用的场景:条件子句

1.3 <where>标签的使用案例

  • 使用 where 关键字

        <!-- 根据性别及班级号查询学生信息   -->
        <!--  select * from student where ssex = ? and cid = ?;  -->
        <select id="getStudentByCondition" resultType="com.cy.mybatis.pojo.entity.Student">
            select * from student
            where
            <if test="ssex != null"> ssex = #{ssex}</if>
            <if test="cid != null"> and c_id = #{cid}</if>
        </select>
    

    上面已经说过了,这种情况会出现一些问题。

    如果我们没有匹配的条件,我们最后这条SQL会变成这样

    select * from student where
    

    如果我们只匹配第二个条件,我们最后这条SQL会变成这样

    select * from student where and c_id = ?
    

    上述两种情况下查询都会造成失败,但是实际我们可以使用如下的方式去解决这些问题

    	<select id="getStudentByCondition" resultType="com.cy.mybatis.pojo.entity.Student">
            select * from student
            where 1=1
            <if test="ssex != null"> and ssex = #{ssex}</if>
            <if test="cid != null"> and c_id = #{cid}</if>
        </select>
    
  • 使用 <where>标签

        <select id="getStudentByCondition" resultType="com.cy.mybatis.pojo.entity.Student">
            select * from student
            <where>
                <if test="ssex != null"> ssex = #{ssex}</if>
                <if test="cid != null"> and c_id = #{cid}</if>
            </where>
        </select>
    

    上述提到的问题,我们可以通过 Mybatis 中的动态SQL标签 <where> 解决,它提供了这些功能,如果子元素中没有匹配条件,则不会插入 where 子句,如果 where 子句 开头为 andor <where> 标签会自动将他们去除。

3. <set> 标签的使用

1. 1 <set> 标签的作用

<set> 主要用于 update 修改中,使用 <set> 标签 相较于 set 关键字好很多,避免很多错误。

1.2 <set>标签的常用场景

经常使用的场景:update 修改语句中

1.3 <set>标签的使用案例

与上述的 <where> 一样,我们不要在 update子句中使用 set 关键字,而要使用 <set> 标签,它能解决一些常见的拼接问题

 	<!--  根据学生id修改名字和班级id(c_id)  -->
    <update id="updateStudentById">
        update student
        <set>
            <if test="sname != null">sname=#{sname},</if>
            <if test="cid != null">c_id=#{cid}</if>
        </set>
        where id=#{id}
    </update>

4. <trim> 标签的使用

1. 1 <trim> 标签的作用

<where><set> 标签不满足我们的需求时,我们可以自定义,这时我们可以使用 <trim> 标签

1.2 <trim>标签的常用场景

自定义

1.3 <trim> 标签的属性

  • prefix
    • <trim> 标签内 SQL 语句 添加前缀
  • suffix
    • <trim> 标签内 SQL 语句 添加后缀
  • prefixOverrides
    • 去除多余的前缀内容,如:prefixOverrides=“OR”,去除 <trim> 标签内 SQL 语句多余的前缀 “OR”
  • suffixOverrides
    • 去除多余的后缀内容,如:prefixOverrides=“,”,去除 <trim> 标签内 SQL 语句多余的后缀 “,”

1.4 <trim>标签的使用案例

	<!--动态插入数据 -->
    <insert id="dynamicInsert">
        insert into student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sname!=null">sname,</if>
            <if test="ssex!=null">ssex,</if>
            <if test="c_id!=null">c_id</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sname!=null">#{sname},</if>
            <if test="ssex!=null">#{ssex},</if>
            <if test="c_id!=null">#{c_id}</if>
        </trim>
    </insert>

5. <foreach> 标签的使用

1. 1 <foreach> 标签的作用

循环遍历

1.2 <foreach>标签的常用场景

循环添加数据,多数据删除

1.3 <foreach> 标签的属性

  • collection
    • 如果想遍历的是 List集合,则参数值为 list
    • 如果想遍历的是 数组,则参数值为 array
  • item
    • item的值是集合/数组每次遍历获取到的元素
  • index
    • 集合和数组中,表示元素的序号
  • open
    • 开始
  • close
    • 结束
  • separator
    • 分隔符
  • nullable
    • 默认为false,开关,若为为false,有可能报错(抛出异常),为true 不抛出异常

1.4 <foreach>标签的使用案例

	<!--  select * from student where sid in(1,2);  -->
    <select id="getStudentByIds" resultType="com.cy.mybatis.pojo.entity.Student">
        <include refid="query"/>
        <where>
            <foreach collection="list" item="item" open="sid in (" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>

6. <choose>,<when>,<otherwise> 标签的使用

1. 1 <choose> <when> <otherwise> 标签的作用

相当于 Java 中分支判断的 if…else if … else…

1.2 <choose> <when> <otherwise> 标签的常用场景

分支判断

1.3 <choose> <when> <otherwise> 标签的属性

  • <when>
    • test
      • <if> 中的 test 属性用法一致

1.4 <choose> <when> <otherwise> 标签的使用案例

	<select id="getStudentByConditions" resultType="com.cy.mybatis.pojo.entity.Student">
        select * from student
        <choose>
            <when test="sname != null">
                and sname like #{sname}
            </when>
            <when test="ssex != null">
                and ssex = #{ssex}
            </when>
            <otherwise>
                and c_id = 1
            </otherwise>
        </choose>
    </select>

猜你喜欢

转载自blog.csdn.net/weixin_46030002/article/details/127486717