Mybatis的<where><trim><set>标签使用和排重

trim标签可以很好地兼容where和set的功能

只要你在trim标签中的prefix中使用对应的关键字,就可以达到相应的作用

所以where和set可以不用啦


先对比一下mybatis中trim的几个参数的作用

prefixOverrides :是取消前面的多余内容,比如where语句中的and

suffixOverrides :是取消后缀的多余内容,比如set中的逗号

先说prefixOverrides

<sql id="whereExcludeIp">
		<if test="exd_id !=null and exd_id !=''">
			AND exi.exd_id = #{exd_id}
		</if>
		<if test="ip_addr_outer !=null and ip_addr_outer !=''">
			AND exi.ip_addr_outer like CONCAT('%',#{ip_addr_outer},'%')
		</if>
		<if test="port_outer !=null and port_outer !=''">
			AND exi.port_outer like CONCAT('%',#{port_outer},'%')
		</if>
		<if test="ip_addr_inner !=null and ip_addr_inner !=''">
			AND exi.ip_addr_inner like CONCAT('%',#{ip_addr_inner},'%')
		</if>
</sql>

使用见下面

<!-- 查询受限ip信息 -->
	<select id="findExcludeIpInfo"  parameterType="Map" resultType="Map">
		<include refid= "selExcludeIp" />
		<include refid= "fromExcludeIp" />
		<trim prefix="WHERE" prefixOverrides="AND|OR">
			<include refid= "whereExcludeIp" /> 
		</where>
	</select>

接下来是suffixOverrides

<update id="updateExcludeIpInfo" parameterType="Map">
		UPDATE tb_exclude_ip
		<trim prefix="SET" prefixOverrides=",">
	        <if test="used_status !=null and used_status !='' ">       
	            used_status = #{used_status},        
	        </if>       
	        <if test="used_status !=null and used_status !='' ">       
	            used_status = #{used_status},        
	        </if>
	    </set>
		WHERE exd_id = #{exd_id}
</update>
上面的代码中的第二个if语句中的逗号可能无用,所以这样可以将它失效。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值