Detailed explanation of the use of mybatis trim tags

Detailed explanation of the use of mybatis trim tags

The trim tag of mybatis is generally used to remove redundant and keywords and commas in SQL statements, or to splice "where", "set" and "values(" and other prefixes, or add ")" and other suffixes before SQL statements, which can be used Operations such as selective insert, update, delete, or conditional query.

The following are the attributes involved in the trim tag:

prefix=add prefix suffix=add suffix

prefixOverrides=Remove prefix suffixOverrides=Remove suffix

Attributes describe
prefix The prefix for sql statement splicing
suffix The suffix for sql statement splicing
prefixOverrides Remove the keyword or character in front of the sql statement, the keyword or character is specified by the prefixOverrides attribute, assuming that the attribute is specified as "AND", when the beginning of the sql statement is "AND", the trim tag will remove the "AND"
suffixOverrides Remove keywords or characters after the sql statement, which are specified by the suffixOverrides attribute

1. Use the trim tag to remove redundant and keywords (compared with where)

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE 
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

What happens if none of these conditions match? In the end, this SQL will become like this:

SELECT * FROM BLOG
WHERE

This causes the query to fail. What if only the second condition matches? This SQL will end up like this:

SELECT * FROM BLOG
WHERE 
AND title like 'someTitle'

You can use the where tag to solve this problem, the where element will only insert the "WHERE" clause if there is at least one child element whose condition returns an SQL clause. Also, if the statement begins with "AND" or "OR", the where element will strip them out as well.

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>

The trim tag can also accomplish the same function, written as follows:

<trim prefix="WHERE" prefixOverrides="AND">
	<if test="state != null">
	  state = #{state}
	</if> 
	<if test="title != null">
	  AND title like #{title}
	</if>
	<if test="author != null and author.name != null">
	  AND author_name like #{author.name}
	</if>
</trim>

2. Use the trim tag to remove extra commas

insert image description here
If the conditions in the red box are not matched, the sql statement will become as follows:

INSERT INTO role(role_name,) VALUES(roleName,)

Insertion will fail with extra commas.
Using the trim tag can solve this problem with a small amount of modification, as follows:
insert image description here
the most important attributes are

suffixOverrides=","

Indicates to remove the extra comma at the end of the sql statement.

At the same time, in order to clarify the relationship between several attributes of trim, the following two xmls are equivalent

<insert id="insertSelective">
   insert into BZ_EVENTINFOGS
   (
      <if test="eventinfoid != null">
        EVENTINFOID,
      </if>
      <if test="eventinfoname != null">
        EVENTINFONAME,
      </if>
   )
   values (
      <if test="eventinfoid != null">
        #{eventinfoid,jdbcType=VARCHAR},
      </if>
      <if test="eventinfoname != null">
        #{eventinfoname,jdbcType=VARCHAR},
      </if>
   )

Equivalent to

<insert id="insertSelective">
   insert into BZ_EVENTINFOGS
   <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="eventinfoid != null">
        EVENTINFOID,
      </if>
      <if test="eventinfoname != null">
        EVENTINFONAME,
      </if>
   <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="eventinfoid != null">
        #{eventinfoid,jdbcType=VARCHAR},
      </if>
      <if test="eventinfoname != null">
        #{eventinfoname,jdbcType=VARCHAR},
      </if>

That is to say at the beginning:

prefix=添加前缀 suffix=添加后缀

prefixOverrides=去掉前缀 suffixOverrides=去掉后缀

Guess you like

Origin blog.csdn.net/qq_27480007/article/details/130894678