Mybatis_trim

via: http://www.cnblogs.com/qiankun-site/p/5758924.html

 

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

  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 values ​​of name and gender are not null, the printed SQL is: select * from user where     name = 'xx' and gender = 'xx'

  The first and does not exist in the place marked in red. The meanings of the above two properties are as follows:

  prefix: prefix      

  prefixoverride: remove the first and or 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 values ​​of name and gender are not null, the printed SQL is: update user set name='xx' , gender='xx'      where id='x'

  There is no comma in the red marked place, and a set prefix and a where suffix are automatically added. The meanings of the above three attributes are as follows, and the meaning of prefix is ​​as above:

  suffixoverride: remove the last comma (it can also be other tokens, like the and in the prefix above)

  suffix: suffix

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567088&siteId=291194637