MyBatis use of Trim tags in dynamic sql

Source: Internet
Author: User

The trim tag is a formatted tag that can complete the function of a set or a where tag, as in the following code:

1.

SELECT * FROM user

<trim prefix= "WHERE" prefixoverride= "and |or" >

<if test= "Name! = null and Name.length () >0" > and name=#{name}</if>

<if test= "Gender! = null and Gender.length () >0" > and gender=#{gender}</if>

</trim>

If the value of name and gender is not NULL, the print SQL is: SELECT * from user where name = ' xx ' and gender = ' xx '

Where the red flag is not present in the first and, the above two properties mean the following:

Prefix: Prefix

Prefixoverride: Remove the first and OR

2.

Update user

<trim prefix= "Set" suffixoverride= "," suffix= "Where id = #{id}" >

<if test= "Name! = null and Name.length () >0" > Name=#{name}, </if>

<if test= "Gender! = null and Gender.length () >0" > and Gender=#{gender}, </if>

</trim>

If the value of name and gender is not NULL, print the SQL: Update user set name= ' xx ', gender= ' xx ' where id= ' x '

There is no comma in the red flag, and a set prefix and a where suffix are added automatically, the meanings of the above three attributes are as follows, with the prefix meaning as above:

Suffixoverride: Remove the last comma (or another tag, as in the previous prefix)

Suffix: suffix

MyBatis use of Trim tags in dynamic sql

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.