Just joined the development industry, know less things, there may be lack of expression, students do not shoot bricks to encourage each other AH.
Ibatis is very useful, when writing SQL statements, you can also use conditional tags such as isnotnull,isequal,isempty to generate dynamically based on property fields. As follows
<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>
The problem arises, and if headimg isnull,status isnotnull, it generates an incorrect SQL statement, one more ","
Update Message_boardset set,status=? where id=? and merchant_id=?
Fortunately Ibatis has dynamic,removefirstprepend tag. Dynamically refers to the dynamic, removefirstprepend refers to the removal of the first prepend. Then change the code to the following:
<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>
In fact, this code removefirstprepend does not work.
The workaround is to add the Prepend property to dynamic with the value "", and note that there is a space between the double quotes. So the correct code is as follows:
<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>
If Headimg isnull,status isnotnull, the resulting SQL statement is
Update Message_board set status=? where id=? and merchant_id=?
My understanding is that when the dynamic tag is not added to the prpend, the extra comma and the preceding code are glued so that removefirstprepend does not know it.
There is no other workaround that I have not found. Online about Removefirstprepend's article is really not much. The above workaround is from: https://issues.apache.org/jira/browse/IBATIS-430