Removefirstprepend Failure resolution Method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.