java: MyBatis dynamic SQL

1. if tag

Format of if tag

<if test="条件">
    SQL片段
</if>

The role of if tag

Splice SQL fragments when the condition is true

For example: query users based on user name and gender. If the user name is not empty, the user name is used as the query condition. If the gender is not empty, the gender is used as the query condition.

    <select id="selectByIf" resultType="User">
        select * from user
            where
            <if test="username != null and username != '' ">
                username like #{username}
            </if>
            <if test="sex != null and sex != '' ">
                and sex = #{sex};
            </if>
    </select>

However, with this way of writing, you will find that if you do not fill in the content of username, such splicing will report an error. Also, if you do not fill in both, an error will also be reported, because there is a where. Such writing is not practical in practical applications. is very inconsistent with

2. where tag

The role of where tag

where needs to serve as the where keyword when there are conditions, and will automatically disappear when there are no query conditions.

  1. Automatically complete the where keyword
  2. Remove redundant and and or keywords
    <select id="selectByIf" resultType="User">
        select * from user
            <where>
                <if test="username != null and username != '' ">
                    username like #{username}
                </if>
                <if test="sex != null and sex != '' ">
                    and sex = #{sex};
                </if>
            </where>
    </select>
3. set tag

In the original modified SQL, add dynamic SQL

    <select id="updateByIf" resultType="User">
        update user set
        <if test="username != null and username !=''">
            username = #{username},
        </if>
        <if test="birthday != null and birthday !=''">
            birthday = #{birthday},
        </if>
        <if test="sex != null and sex !=''">
            sex = #{sex},
        </if>
        <if test="address != null and address !=''">
            address = #{address},
        </if>
            where id = #{id};
    </select>

You will find that there will be an extra comma in the SQL statement when the test is run.

==>  Preparing: update user set username = ?, sex = ?, where id = ?;

So we will choose to use the Set tag to remove the extra commas

    <select id="updateByIf" resultType="User">
        update user
        <set>
            <if test="username != null and username !=''">
                username = #{username},
            </if>
            <if test="birthday != null and birthday !=''">
                birthday = #{birthday},
            </if>
            <if test="sex != null and sex !=''">
                sex = #{sex},
            </if>
            <if test="address != null and address !=''">
                address = #{address},
            </if>
        </set>
            where id = #{id};
    </select>
4. foreach tag
attributes of foreach tag effect
collection parameter name
item Set a variable name to represent each traversed element
separator Traverse the content added by an element
#{variable name} Use ? placeholder first, then assign value to ?
open Add characters once before traversing
close Add characters once after traversal

Delete users in batches

    <delete id="deleteByArray" >
        delete from user where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

Note: The ids here need to be annotated on the interface, otherwise they will use the default name.

    void deleteByArray(@Param("ids") int[] ids);
5. choose tag

Equivalent to switch in java

attributes of choose tag effect
when match a condition
otherwise Executed when all conditions do not match

Set query gender default

    <select id="selectBySex" resultType="org.example.pojo.User">
        select * from user
        <where>
            <choose>
                <when test="sex == 1">
                    sex = '男'
                </when>
                <when test="sex == 0">
                    sex = '女'
                </when>
                 <otherwise>
                     sex = '女'
                 </otherwise>
            </choose>
        </where>
    </select>

Guess you like

Origin blog.csdn.net/weixin_53961667/article/details/134352464