removeFirstPrepend失效解决方法

刚加入开发行业,知道的东西少,可能表达上会有欠缺,同学们不要拍砖要相互勉励啊。

ibatis很好用,写sql语句时还可以用isNotNull,isEqual,isEmpty等条件标签根据property字段动态生成。如下

<update id="update" parameterClass="MessageBoard">
	update 
		message_board
	set
		<isNotNull property="headImg" >head_img=#headImg#</isNotNull>
		<isNotNull prepend="," property="status">status=#status#</isNotNull>
	where 
		id=#id# and merchant_id=#merchantId#
</update>

问题出现了,如果 headImg isNull,status isNotNull,就会生成错误的sql语句,多了一个"," 

update message_boardset set,status=? where id=? and merchant_id=?

还好ibatis有dynamic,removeFirstPrepend标签。从字面来看dynamic是指动态,removeFirstPrepend是指移除第一个prepend。那么把代码改成如下: 

<update id="update" parameterClass="MessageBoard">
	update 
		message_board
	set
		<dynamic>
			<isNotNull property="headImg" >head_img=#headImg#</isNotNull>
			<isNotNull prepend="," property="status" removeFirstPrepend="true">status=#status#</isNotNull>
		</dynamic>
	where 
		id=#id# and merchant_id=#merchantId#
</update>

其实这样的代码removeFirstPrepend并没有起效。
解决办法是:给dynamic 加prepend属性,值是“ ”,注意双引号之间有个空格。所以正确的代码如下:

<update id="update" parameterClass="MessageBoard">
	update 
		message_board
	set
		<dynamic prepend=" ">
			<isNotNull property="headImg" >head_img=#headImg#</isNotNull>
			<isNotNull prepend="," property="status" removeFirstPrepend="true">status=#status#</isNotNull>
		</dynamic>
	where 
		id=#id# and merchant_id=#merchantId#
</update>

如果 headImg isNull,status isNotNull,产生的sql语句是

update message_board set status=? where id=? and merchant_id=?

我的理解是在没有给dynamic标签加prpend时,多余的逗号和前面代码粘牢了以至于removeFirstPrepend不认识它。

有没有其他其他解决方法,我没有查到。网上关于removeFirstPrepend的文章真心不多。以上解决方法来自:https://issues.apache.org/jira/browse/IBATIS-430

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值