• mybatis03


    一、mybatis动态sql

    动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦

    ①动态SQL:if 语句

    根据 stu_name 和 stu_sex 来查询数据。如果stu_name为空,那么将只根据stu_sex来查询;反之只根据stu_name来查询

    首先不使用 动态SQL 来书写

    1. <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
    2. select * from user where stu_name=#{stu_name} and stu_sex=#{stu_sex}
    3. select>

    可以发现,如果 #{stu_name} 为空,那么查询结果也是空,如何解决这个问题呢?使用 if 来判断

    1. <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
    2. select * from student where
    3. <if test="stu_name != null">
    4. stu_name=#{stu_name}
    5. if>
    6. <if test="stu_sex!= null">
    7. and stu_sex=#{stu_sex}
    8. if>
    9. select>

    这样写我们可以看到,如果 sex 等于 null,那么查询语句为 select * from student where stu_name=#{stu_name},但是如果stu_name 为空呢?那么查询语句为 select * from user where and stu_sex=#{sex},这是错误的 SQL 语句,如何解决呢?

    ②if+where 语句

    1. <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
    2. select * from student
    3. <where>
    4. <if test="stu_name != null">
    5. stu_name=#{stu_name}
    6. if>
    7. <if test="stu_sex!= null">
    8. and stu_sex=#{stu_sex}
    9. if>
    10. where>
    11. select>

    这个where标签会知道如果它包含的标签中有返回值的话,它就插入一个where。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉

    ③动态SQL:if+set 语句

    上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词

    1. <update id="updateStudentById" parameterType="cn.kgc.mybatis.pojo.Student">
    2. update student stu
    3. <set>
    4. <if test="stuname != null and stuname != ''">
    5. stu.stu_name = #{stuName},
    6. if>
    7. <if test="stusex != null and stusex != ''">
    8. stu.stu_sex = #{stuSex}
    9. if>
    10. set>
    11. where id=#{id}
    12. update>
    • 如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?

    • 如果第一个条件不为空,那么 sql 语句为:update user u set u.username = ? ,u.sex = ? where id=?

    ④动态SQL:choose(when,otherwise) 语句

    有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句

    1. <select id="selectStudentByChoose" resultType="com.ys.po.User" parameterType="com.ys.po.User">
    2. select * from student
    3. <where>
    4. <choose>
    5. <when test="id !='' and id != null">
    6. id=#{id}
    7. when>
    8. <when test="username !='' and username != null">
    9. and username=#{username}
    10. when>
    11. <otherwise>
    12. and sex=#{sex}
    13. otherwise>
    14. choose>
    15. where>
    16. select>

    这里我们有三个条件,id,username,sex,只能选择一个作为查询条件

    • 如果 id 不为空,那么查询语句为:select * from user where id=?

    • 如果 id 为空,那么看username 是否为空,如果不为空,那么语句为 select * from user where username=?;

    • 如果 username 为空,那么查询语句为 select * from user where sex=?

    ⑤动态SQL:trim 语句

    trim标记是一个格式化的标记,可以完成set或者是where标记的功能

    用 trim 改写上面的 if+where 语句

    1. <select id="selectStudent" resultType="student" parameterType="cn.kgc.mybatis.pojo.Student">
    2. select * from user
    3. <trim prefix="where" prefixOverrides=" and | or">
    4. <if test="stu_name != null">
    5. stu_name=#{stu_name}
    6. if>
    7. <if test="stu_sex!= null">
    8. and stu_sex=#{stu_sex}
    9. if>
    10. trim>
    11. select>
    • prefix:前缀  

    • prefixoverride:去掉第一个and或者是or

    用 trim 改写上面的 if+set 语句

    1. <update id="updateStudentById" parameterType="cn.kgc.mybatis.pojo.Student">
    2. update student stu
    3. <trim prefix="set" suffixOverrides=",">
    4. <if test="stuname != null and stuname != ''">
    5. stu.stu_name = #{stuName},
    6. if>
    7. <if test="stusex != null and stusex != ''">
    8. stu.stu_sex = #{stuSex}
    9. if>
    10. trim>
    11. where id=#{id}
    12. update>
    • suffix:后缀  

    • suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

    ⑥动态SQL: foreach 语句

    需求:我们需要查询 user 表中 id 分别为1,2,3的用户,sql语句:

    1. select * from user where id=1 or id=2 or id=3
    2. select * from user where id in (1,2,3)

    用 foreach 来改写 select * from user where id=1 or id=2 or id=3

    1. <select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
    2. select * from user
    3. <where>
    4. <foreach collection="ids" item="id" separator="or">
    5. id=#{id}
    6. foreach>
    7. where>
    8. select>

    用 foreach 来改写 select * from user where id in (1,2,3)

    二、动态SQL: SQL 片段

    有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

    假如我们需要经常根据用户名和性别来进行联合查询,那么我们就把这个代码抽取出来,如下:

    1. <sql id="selectUserByUserNameAndSexSQL">
    2. <if test="username != null and username != ''">
    3. AND username = #{username}
    4. if>
    5. <if test="sex != null and sex != ''">
    6. AND sex = #{sex}
    7. if>
    8. sql>
    1. <select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
    2. select * from user
    3. <trim prefix="where" prefixOverrides="and | or">
    4. <include refid="selectUserByUserNameAndSexSQL">include>
    5. trim>
    6. select>

            ①、最好基于 单表来定义 sql 片段,提高片段的可重用性

      ②、在 sql 片段中最好不要包括 where

  • 相关阅读:
    高校教学教务管理系统文档
    OCTAFX滑点滑到令人发指 现在出金也不给出金是黑平台无疑了
    四、Flask进阶
    vue/html input 读取 json数据
    【Log】为类中的所有日志打印添加前缀
    51单片机学习笔记1 简介及开发环境
    Soul App Android一二三面凉经(2024)
    史上最全架构师知识图谱(纯干货)
    ubuntu配置cuda环境
    log4j.properties支持不同包日志分文件打印
  • 原文地址:https://blog.csdn.net/lmdbhf/article/details/128062544