mybatis动态sql之trim用法

目录

trim标签有四个属性

使用trim标签去除多余的and关键字 

使用trim标签结合case when实现批量更新

Demo1

Demo2


trim标签有四个属性

1.prefix,prefixOverrides,suffix,suffixOverrides
  • prefix:给sql语句拼接的前缀
  • suffix:给sql语句拼接的后缀
  • prefixOverrides:去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND"
  • suffixOverrides:去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定

使用trim标签去除多余的and关键字 

<select id="findActiveBlogLike"
  resultType="Blog">
 SELECT * FROM BLOG 
 WHERE 
 <if test="state != null">
 state = #{state}
 </if> 
 <if test="title != null">
 AND title like #{title}
 </if>
 <if test="author != null and author.name != null">
 AND author_name like #{author.name}
 </if>
</select>
  • 如果这些条件没有一个能匹配上会发生什么?最终这条 SQL 会变成这样:
SELECT * FROM BLOG
WHERE
  • 这会导致查询失败。如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:
SELECT * FROM BLOG
WHERE
AND title like ‘someTitle'
  • 使用 trim标签可以完成where标签相同的功能
<trim prefix="WHERE" prefixOverrides="AND">
	<if test="state != null">
	 state = #{state}
	</if> 
	<if test="title != null">
	 AND title like #{title}
	</if>
	<if test="author != null and author.name != null">
	 AND author_name like #{author.name}
	</if>
</trim>

使用trim标签结合case when实现批量更新

Demo1

@Update({
    <script>+
        UPDATE xxxxxx
        //去掉最后的,
        <trim prefix='set', suffixOverrides=','>+
             //以end,结尾
             <trim prefix='MGNIP = case', suffix='end,'>+
                 <foreach collection="list" index="index" item="item"> +
                    <if test='item.mgntIp' != null and item.mgntIp != \"\"> +
                        when ID = #{item.id}+
                        then #{item.mgntIp}
                    </if> +
                 </foreach> + 
        </trim> +
       
        <trim prefix='DEVICENAME= case', suffix='end,'>+
              <foreach collection="list" index="index" item="item"> +
                  <if test='item.deviceName' != null and item.deviceName != \"\"> +
                     when ID = #{item.id}+
                      then #{item.deviceName}
                  </if> +
              </foreach> + 
        </trim>
        <where> +
             <foreach collection="list" index="index" item="item" separator = " or "> +
                  ID = #{item.id} and TENANTID = #{item.tenantId}
              </foreach> + 
        </where>
    </script>
})

int updateUnMgntDevices(List<UnMgntDevice> UnMgntDevices);

Demo2

@Update({

UPDATE hr_emp_employee
        SET `on_job` = CASE
        WHEN `status` IN
        <foreach collection="list" index="index" item="id" open="(" separator="," close=")">
         #{id}
        </foreach> THEN 0
        ELSE 1
        END
        WHERE 1=1


})
    
void updateEmp(@Param("list") List<String> list);
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值