The dynamic sql mybatis of java

1. if the interpretation of conditions are met, we will meet together with the sql statement.

<select id="findUser" parameterType="Map" resultType="User">
        select * from t_user where 1=1 
        <if test="name!=null">
            and name like '${name}%'
        </if>
</select>

2. choose, when, otherwise ----- when you can have multiple

<select id="findUser" parameterType="Map" resultType="User">
        select * from t_user where 1=1 
        <choose>
            <when test="name!=null">
                and name like '${name}%'
            </when>
            <otherwise>
                and age=23
            </otherwise>
        </choose>
    </select>

3.where label to determine whether conditions established

If the conditions are plus where, not if there is no increase.

where element knows to insert "where" label if it contains has returned content words.

Further, if the content is returned to "AND" or "OR" at the beginning, it will "AND" Get "OR" removed.

<select id="findUser" parameterType="Map" resultType="User">
        select * from t_user 
        <where>
            <if test="name!=null">
                 name like '${name}%'
            </if>
            <if test="age>23">
                and age=23
            </if>
        </where>
    </select>

4. trim ---- overrides property using the text-delimited list of the pipeline to overwrite, but it also can not ignore the blank.

This results in the removal of the specified character attribute overrides, the designated inserting a leading prefix attribute characters.

<select id="findUser" parameterType="Map" resultType="User">
        select * from t_user 
        <trim prefix="order by" prefixOverrides="AND |OR ">
            <if test="age>23">
                and age desc
            </if>
        </trim>
    </select>

5. foreach ---- frequently used function is to set the iteration, conditionals commonly used in IN.

<select id="findUser" parameterType="list" resultType="User">
        select * from t_user 
        <where>
            id in 
            <foreach collection="list" item="var" 
            open="(" close=")" separator=",">
                #{var}
            </foreach>
        </where>
    </select>

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11297625.html