动态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>
    

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>
    
  • 第一个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>
    
  • 加前缀时,改标签可以动态判断,当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>
    
  • 可以自动去掉逗号 “,”,并且添加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>
  • 三个都为空走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>
    

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>
    

sql标签和include标签

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

  • <!--声明一个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" >
            <!--插入这个片段通过id-->
            <include refid="selectColumn" />
        </trim>
    </select>
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值