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