Basic usage of dynamic sql statement

 

1.if statement

If empno is not empty, add AND empno = # {empno} after the WHERE parameter, there is 1 = 1, so even if empno is null, there will be no error after WHERE.

Mapping file

<select id="getEmpById2" resultType="emp"> 
        SELECT * FROM emp WHERE 1=1
        <if test="empno != null">
            AND empno = #{empno}
        </if>   
    </select>

EmpMapper interface

public Emp getEmpById2(@Param("empno")Integer empno) throws IOException;

Sometimes we don't want to apply all the conditions, but just want to choose one from multiple options. When using the if tag, as long as the expression in the test is true, the condition in the if tag will be executed. MyBatis provides the choose element. The if tag is related to (and), and choose is related to or.

2. Where statement and Choose (when, otherwise)

1.Where empno and ename are null, then where will not appear in the sql statement.
2. The choose tag determines whether the test condition in the internal when tag is satisfied in order. If one of them is true, then choose ends. When the conditions of all when in choose are not satisfied, then execute SQL in otherwise. Similar to Java's switch statement, choose is switch, when is case, otherwise is default.

Mapping file

<select id="getEmpById3" resultType="emp" parameterType="emp">
        SELECT * FROM EMP 
        <where>
            <choose>
                <when test="empno != null">
                    AND empno like #{empno}
                </when>
                <when test="ename != null">
                    AND ename like #{ename}
                </when>
                <otherwise>
                    AND  job = "zz"
                </otherwise>
            </choose>
        </where>
    </select>

3.set statement

Set is mainly used to solve the update problem.
Mapping file

<update id="updateEmprById2" parameterType="emp">
        UPDATE emp
        <set>
            <if test="ename!=null"> ename=#{ename},</if>
            <if test="job!=null"> job=#{job},</if>
        </set>
        <where>
            <if test="empno!=null">
                empno=#{empno};
            </if>
        </where>
    </update>

4.trim

The trim mark is a formatted mark that can complete the function of set or where mark.
Related attributes:
Prefix: Prefix.
prefixOverrides: Remove the first specified content.
suffix: suffix.
suffixoverride: remove the last specified content.
Mapping file

<!-- 代替where -->
    <select id="getEmpById4" resultType="emp" parameterType="emp">
        SELECT * FROM emp
        <!-- <where> <if test="username!=null"> and name = #{username} </if> </where> -->
        <trim prefix="where" prefixOverrides="AND |OR ">
            <if test="empno != null">
                and empno = #{empno}
            </if>
            <if test="ename!=null">
                AND ename = #{ename}
            </if>
        </trim>
    </select>

Mapping file

<!-- 代替set -->
    <update id="updateEmprById3" parameterType="emp">
        update emp
        <trim prefix="set" suffixOverrides=",">
            <if test="ename!=null">
                ename = #{ename},
            </if>
            <if test="job != null">
                job = #{job}
            </if>
        </trim>
        <trim prefix="where" prefixOverrides="AND |OR ">
            <if test="empno != null">
                and empno = #{empno}
            </if>
        </trim>
    </update>

5.foreach statement

Foreach is used to traverse, and the traversed object can be an array or a collection.
Related attributes:
Collection: There are three values ​​for the collection attribute, namely list, array, and map.
Open: prefix.
Close: suffix.
Separator: Separator, which indicates what separates each element during iteration.
Item: Represents the alias of each element during the iteration.
Index: Use a variable name to indicate the index position of the current loop.
Mapping file

    <insert id="addEmp6">
        insert into emp(ename,job)values
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.ename},#{emp.job})
        </foreach>
    </insert>

6.SQL block

Mapping file

<!-Define repeated SQL content-> 
    <sql id = "baseSql"> 
        empno, ename, job
     </ sql> 
    
    <!-Use include to introduce sql block-> 
    <select id = "selEmp1" resultType = "emp"> 
        select
         <include refid = "baseSql" /> 
         from emp
     </ select>

 

Guess you like

Origin www.cnblogs.com/JonaLin/p/12706411.html