The difference and use of where tag and trim tag in mybatis

1. Multiple query conditions

Under multiple query conditions, where 1 = 1 will be added in front of it because of the need to splice sql statements

	<select id="queryList" resultMap="BasBusinessTypeExtendsMap">
	    select
	    <include refid="Base_Column_List"></include>
	    from bas_business_type_setting
	    where 1 = 1
	    <if test="params.dictType != null">
	        and dict_type = #{
    
    params.dictType}
	    </if>
	    <if test="params.libraryType != null">
	        and library_type = #{
    
    params.libraryType}
	    </if>
	</select>

Although the above 1=1 writing method can realize the function, there is a risk of SQL injection, and it may cause index invalidation and full table scanning, so the where tag can be used instead of where 1=1

2. The use of where tags

Use the where tag instead of where 1=1

    <select id="count" resultType="java.lang.Long">
        select count(1)
        from bas_building
        <where>
            <if test="params.id != null">
                and id = #{
    
    params.id}
            </if>
            <if test="params.idList != null and params.idList.size() > 0 " >
                and id in
                <foreach collection="params.idList" item="idItem" index="index" open="(" close=")" separator=",">
                    #{
    
    idItem}
                </foreach>
            </if>
            <if test="params.buildingNo != null and params.buildingNo != ''">
                and building_no = #{
    
    params.buildingNo}
            </if>
        </where>
    </select>

[Function]
Automatically remove "AND" or "OR" in the first condition

Note:
1. According to the standard writing method, the AND in the first if tag should not be written, but it will not report an error even if it is written during development. Because the where tag will automatically remove the first AND link. However, within the if tag after the second one, there must be an AND link.
2. If none of the conditions are met, return all entries.

3. The use of trim tags

The function of this tag is similar to where, and additionally provides prefix and suffix functions. The specific usage is as follows:

    <select id="count" resultType="java.lang.Long">
        select count(1)
        from bas_building
        <trim prefix="where" prefixOverrides="and|or">
            <if test="params.id != null">
                and id = #{
    
    params.id}
            </if>
            <if test="params.idList != null and params.idList.size() > 0 ">
                and id in
                <foreach collection="params.idList" item="idItem" index="index" open="(" close=")" separator=",">
                    #{
    
    idItem}
                </foreach>
            </if>
            <if test="params.buildingNo != null and params.buildingNo != ''">
                and building_no = #{
    
    params.buildingNo}
            </if>
        </trim>
    </select>

[Function]
1. You can use trim instead of where tag.
2. The attribute prefix="where", means: add the prefix where.
3. The attribute suffix=")", means: add suffix).
4. Attribute prefixOverrides="and|or", prefix override: automatically override the first and or or.
5. Attribute suffixOverrides="", suffix override: remove redundant characters behind the entire string.

4. trim extension

The trim tag has more usage scenarios than where, as follows:

<if test="params.businessTypeList != null and params.businessTypeList.size() > 0 ">
    <trim prefix="and ( " prefixOverrides="AND|OR" suffix=" )">
        <foreach collection="params.businessTypeList" item="idItem" index="index">
            OR FIND_IN_SET( #{
    
    idItem}, space_types )
        </foreach>
    </trim>
</if>

When looping, splice multiple sql conditions, use the prefixOverrides attribute of the trim tag to overwrite the first and or or, add "and (" to the suffix, and ")" to the prefix, and dynamically generate a sql that meets multiple conditions.

Guess you like

Origin blog.csdn.net/qq_42547733/article/details/129023139