Java_Mybatis_动态SQL

一、动态SQL

1.概述

  • 动态SQL: 是 MyBatis 的强大特性之一,解决拼接动态SQL时候的难题,提高开发效率
  • 分类
    • if
    • choose(when,otherwise)
    • trim(where,set)
    • foreach

2.if

  • 做 where 语句后面条件查询的,if 语句是可以拼接多条

  • 需求:根据学生name 做模糊查询

  • 代码

    • mapper.xml

      <select id="selectLikeName" resultType="cn.sycoder.domain.Student">
              select id,name,age
              from student
              where age = 19
              <if test="name != null">
                  and name like concat(#{name},'%')
              </if>
      </select>
      
    • java 代码

       List<Student> selectLikeName(String name);
      

      在这里插入图片描述

3.choose、when、otherwise

  • 概述:不想使用所有条件时候,他们可以从多个条件中选择一个使用,相当于java 的 if … else if … else

  • 需求:按年龄19查找,如果id 不空按id 查找,名字不空按名字查找,否则按班级id 查找

    • mapper.xml

      <select id="selectChoose" resultType="cn.sycoder.domain.Student">
              select <include refid="baseSql"/>
              from student
              where age = 19
              <choose>
                  <when test="id != null">
                      and id = #{id}
                  </when>
                  <when test="name != null">
                      and name like concat(#{name},'%')
                  </when>
                  <otherwise>
                      and class_id = #{clsId}
                  </otherwise>
              </choose>
          </select>
      
    • mapper

      List<Student> selectChoose(@Param("id") Long id,@Param("name")String name
                  ,@Param("clsId") Long clsId);
      
    • 传入 id 参数

      在这里插入图片描述

    • 不传 id 参数,传入name = ‘z’

      在这里插入图片描述

    • 不传入 id 参数和 name 参数

      在这里插入图片描述

4.trim、where、set

4.1trim

  • trim : 用于去掉或者添加标签中的内容

  • prefix:可以在 trim 标签内容前面添加内容

    在这里插入图片描述

  • prefixOverrides:可以覆盖前面的某些内容

    在这里插入图片描述

  • suffix:在 trim 标签后面添加内容

    在这里插入图片描述

  • suffixOverrides:去掉 trim 标签内容最后面的值

    在这里插入图片描述

4.2where

  • where 后面直接跟 if

    在这里插入图片描述

  • age null

    在这里插入图片描述

  • 使用了 where 标签之后,解决了这些问题

    在这里插入图片描述

4.3set

  • set:set 元素可以用于动态包含需要更新的列

  • mapper.xml

    <update id="updateSet">
            update student
            <set>
                <if test="name != null">
                    name = #{name},
                </if>
                <if test="age != null">
                    age = #{age},
                </if>
            </set>
            <where>
                <if test="id != null">
                    id = #{id}
                </if>
            </where>
        </update>
    
    void updateSet(@Param("age") Integer age,@Param("name")String name
                ,@Param("clsId") Long clsId,@Param("id")Long id);
    

    在这里插入图片描述

5.foreach

  • foreach :用于对集合遍历。 动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)

  • 查询 id 在 1,3,4 之间的学生信息

  • mapper.xml

    <select id="selectForeach" resultType="cn.sycoder.domain.Student">
            select * from student
            <where>
                <foreach collection="ids"  item="id" index="i" open="id in(" close=")" separator=",">
                    #{id}
                </foreach>
            </where>
        </select>
    
    List<Student> selectForeach(@Param("ids") List<Long> ids);
    

    在这里插入图片描述

    在这里插入图片描述

    • collection:传参的数组集合
    • item:遍历拿到的每一个元素
    • index:索引
    • open : foreach 标签内容的开始符
    • close : foreach 标签内容的结束符
    • separator:分隔符
    • 取值取的就是 item 的元素值
    • 注意:当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。

6.script

  • script:要在带注解的映射器接口类中使用动态 SQL,可以使用 script 元素。

  • 使用注解操作 mybatis

  • 需求:查询所有的学生信息,用注解方式实现

    @Select("select * from student")
        List<Student> selectAll();
    
  • 更新学生信息,使用 script 标签

    @Update({
                "<script>",
                "update student",
                "  <set>",
                "    <if test='name != null'>name=#{name},</if>",
                "    <if test='age != null'>age=#{age},</if>",
                "    <if test='clsId != null'>class_id=#{clsId},</if>",
                "  </set>",
                "where id=#{id}",
                "</script>"
        })
        void updateStu(@Param("age") Integer age,@Param("name")String name
                ,@Param("clsId") Long clsId,@Param("id")Long id);
    

7.bind

  • bind 元素允许你在 OGNL 表达式以外创建一个变量,并将其绑定到当前的上下文。

  • 需求:通过用户name 进行模糊查询

    <select id="listLike" resultType="cn.sycoder.domain.Student">
            <bind name="ret" value="'%' + name + '%'"/>
            select * from student
            where name like #{ret}
        </select>
    

    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值