Mybatis <include> <trim> label detailed explanation

1. Label <include>

Tags are used to introduce a certain SQL fragment as a whole into the current SQL statement, and can encapsulate reusable SQL statement fragments for easy management and maintenance. When using labels, you need to pay attention to the following points:

grammatical format

<include refid="SQL片段ID"/>

Among them, the refid attribute points to the ID of the SQL fragment to be imported, for example:

<select id="selectUsers" resultMap="userResultMap">
  SELECT * FROM users
  <include refid="whereClause" />
</select>

<sql id="whereClause">
  <where>
    <if test="name != null">
      AND name = #{
    
    name}
    </if>
    <if test="age != null">
      AND age = #{
    
    age}
    </if>
  </where>
</sql>

In the above example, the label defines a SQL fragment, which contains one label and two labels, which are used to dynamically generate query conditions. The tag's refid attribute references the SQL fragment and includes it in the tag.

Scope of SQL fragments
In the XML configuration file of Mybatis, there are two scopes of SQL fragments:

(1) Global scope: SQL fragments defined outside the tags can be used in the entire XML file.

(2) Local scope: SQL fragments defined inside the , , or tags are only used in the SQL statement of the current tag.

Therefore, when defining SQL fragments, it is necessary to select an appropriate scope according to the actual situation to avoid naming conflicts or unnecessary memory usage.

2. <trim> tag

Labels are also used to dynamically generate SQL statements, often used to remove redundant SQL keywords or conditional statements. When using labels, you need to pay attention to the following points:

grammatical format

<trim prefix="前缀" suffix="后缀" prefixOverrides="需要去除的前缀" suffixOverrides="需要去除的后缀">
  SQL语句
</trim>

Among them, the prefix attribute indicates the prefix added before the SQL statement, the suffix attribute indicates the suffix added after the SQL statement, the prefixOverrides attribute indicates the prefix that needs to be removed, and the suffixOverrides attribute indicates the suffix that needs to be removed. Suppose we have a SQL statement that queries users, There may be some redundant AND or OR conditions, we can use the trim tag to remove these redundant parts, as follows:

<select id="getUserList" resultType="User">
  SELECT * FROM user
  <trim prefix="WHERE" prefixOverrides="AND | OR">
    <if test="id != null">
      AND id = #{
    
    id}
    </if>
    <if test="name != null">
      AND name = #{
    
    name}
    </if>
    <if test="age != null">
      AND age = #{
    
    age}
    </if>
  </trim>
</select>

In this example, we use the trim tag to remove redundant WHERE, AND, and OR keywords that may appear in the SQL statement. If id, name and age are all empty, then the final generated SQL statement is "SELECT * FROM user", if only id is not empty, then the final generated SQL statement is "SELECT * FROM user WHERE id = ?", if Only name and age are not empty, then the final generated SQL statement is "SELECT * FROM user WHERE name = ? AND age = ?", if id, name and age are not empty, then the final generated SQL statement is "SELECT * FROM user WHERE id = ? AND name = ? AND age = ?".

Guess you like

Origin blog.csdn.net/weixin_44917334/article/details/129402436