• 动态SQL


    动态SQL

    • 可以根据具体的参数条件,来对SQL语句进行动态拼接
    • 比如在以前的开发中,由于不确定查询参数是否存在,许多人会使用类似于where 1 = 1 来作为前缀,然后后面用AND 拼接要查询的参数,这样,就算要查询的参数为空,也能够正确执行查询,如果不加1 = 1,则如果查询参数为空,SQL语句就会变成SELECT * FROM student where,SQL不合法。

    if标签

    • if标签中test属性时必须的

    • if标签中test属性的值是true或者false,true则进行拼接,false则反之

    • test属性中可以使用的是:

      • 当使用了@Param注解后,那么test中出现的是@Param注解中的value
      • 当没有使用@Param注解,那么test中出现的是:param1,param2等等
      • 当使用了POJO,那么test中的是POJO类的属性名
    • 注意test里面用的是and表示并且

    • 例如:

      <select id="selectByMultiCondition" resultType="car">
          select * from t_car
          where 1 = 1
              <if test="brand != null and brand != ''">
                  and brand like "%"#{brand}"%"
              if>
              <if test="guidePrice != null and guidePrice != ''">
                  and guide_price > #{guidePrice}
              if>
              <if test="carType != null and carType != ''">
                  and car_type = #{carType}
              if>
      
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    where标签

    • 让where字句更加动态智能

      • 所有条件都为空时,where标签保证不会生成where字句
      • 自动去除某些条件前面多余的and或or
      • where自动生成
    • <select id="selectByMultiConditionWithWhere" resultType="car">
          select * from t_car
          <where>
              <if test="brand != null and brand != ''">
                  and brand like "%"#{brand}"%"
              if>
              <if test="guidePrice != null and guidePrice != ''">
                  and guide_price > #{guidePrice}
              if>
              <if test="carType != null and carType != ''">
                  and car_type = #{carType}
              if>
          where>
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 第一个if标签中的and可以自动去掉

    trim标签

    • 在这里插入图片描述

    •     <select id="selectByMultiConditionWithTrim" resultType="car">
              select * from t_act
              <trim prefix="where" suffixOverrides="and|or" >
                  <if test="brand != null and brand != ''">
                      brand like "%"#{brand}"%" and
                  if>
                  <if test="guidePrice != null and guidePrice != ''">
                      guide_price > #{guidePrice} and
                  if>
                  <if test="carType != null and carType != ''">
                      car_type = #{carType}
                  if>
              trim>
          select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 加前缀时,改标签可以动态判断,当if里面全部不成立时,where不会加上去。

    set标签

    • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-twDNDO9Z-1669533417960)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1669518854852.png)]

    • <update id="updateBySet">
          update t_car
          <set>
              <if test="carNum != null and carNum != ''">
                  car_num = #{carNum},
              if>
              <if test="brand != null and brand != ''">
                  brand = #{brand},
              if>
              <if test="guidePrice != null and guidePrice != ''">
                  guide_Price = #{guidePrice},
              if>
              <if test="produceTime != null and produceTime != ''">
                  produce_time = #{produceTime},
              if>
              <if test="carType != null and carType != ''">
                  car_type = #{carType},
              if>
          set>
          where
              id = #{id}
      update>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 可以自动去掉逗号 “,”,并且添加set

    choose when otherwise标签

    • [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vl9vFDPg-1669533417961)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1669530264925.png)]

    <select id="selectByChoose" resultType="car">
        select * from t_car
        <where>
            <choose>
                <when test="brand != null and brand != ''">
                    brand like "%"#{brand}"%"
                when>
                <when test="guidePrice != null and guidePrice != ''">
                    guide_price > #{guidePrice}
                when>
                <otherwise>
                    car_type = #{carType}
                otherwise>
            choose>
        where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 三个都为空走otherwise

    foreach标签之批量删除

    • foreach标签的属性:

      • collection:指定数组或者集合
      • item:代表数组或集合中的元素
      • separator:循环之间的分隔符
      • open:foreach循环拼接的所有sql语句的最前面以什么开始。
      • close:foreach循环拼接的所有sql语句的最前面以什么结束
    • <delete id="deleteByIds">
          delete from t_car where id in
          <foreach collection="ids" item="id" separator="," open="(" close=")">
              #{id}
          foreach>
      delete>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    foreach标签之批量插入

    • <insert id="insertBatch">
          insert into t_car values
          <foreach collection="cars" item="car" separator=",">
              (null ,#{car.carType}, #{car.brand}, #{car.carPrice}, #{car.produceTime}, #{car.carType})
          foreach>
      insert>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

    sql标签和include标签

    • 声明sql片段提高复用性,用的较少

    • 
      <sql id="selectColumn">
          <if test="brand != null and brand != ''">
              brand like "%"#{brand}"%" and
          if>
          <if test="guidePrice != null and guidePrice != ''">
              guide_price > #{guidePrice} and
          if>
          <if test="carType != null and carType != ''">
              car_type = #{carType}
          if>
      sql>
      
      <select id="selectByMultiConditionWithTrim" resultType="car">
          select * from t_car
      
          <trim prefix="where" suffixOverrides="and|or" >
              
              <include refid="selectColumn" />
          trim>
      select>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
  • 相关阅读:
    Spring之Bean的自动装配
    网络安全 — 零信任网络访问(ZTNA)
    MongoDB实战:应用场景以及Spring和mongodb的整合
    苹果的司法总监“知法犯法,监守自盗”,涉嫌操控股市被捕
    docker安装elastic search和kibana
    Linux下线程间通讯---读写锁和条件变量
    一文讲清楚密评中的数据库存储加密 安当加密
    Verilog HDL语言基础知识
    Python如何在日志中隐藏明文密码
    前后端分离项目,vue+uni-app+php+mysql在线考试系统(H5移动项目) 开题报告
  • 原文地址:https://blog.csdn.net/qq_52025040/article/details/128064794