您的位置:首页 > 编程语言 > PHP开发

removeFirstPrepend失效解决方法

2014-03-12 14:44 134 查看
刚加入开发行业,知道的东西少,可能表达上会有欠缺,同学们不要拍砖要相互勉励啊。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息