欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中MyBatis的動態(tài)語句詳解

 更新時間:2023年08月02日 10:23:58   作者:碼匠_CodeArtist  
這篇文章主要介紹了Java中MyBatis的動態(tài)語句詳解,動態(tài) SQL 是 MyBatis 的強大特性之一,通過不同參數(shù)生成不同的 SQL,可以動態(tài)地對數(shù)據(jù)持久層進行操作,而不需要每個數(shù)據(jù)訪問操作都要進行手動地拼接 SQL 語句,需要的朋友可以參考下

一、 概述

動態(tài) SQL 是 MyBatis 的強大特性之一,通過不同參數(shù)生成不同的 SQL,可以動態(tài)地對數(shù)據(jù)持久層進行操作,而不需要每個數(shù)據(jù)訪問操作都要進行手動地拼接 SQL 語句。

二、動態(tài)語句

1. if

<if> 語句可以根據(jù)條件指定 SQL 部分,例如 where 條件。

查詢數(shù)據(jù)接口如下:

public interface UserSqlMapper {
    List<User> selectByUsername(@Param("username") String username, @Param("status") Integer status);
}

對應(yīng)映射語句如下:

<select id="selectByUsername" resultType="User">
    SELECT * FROM t_user WHERE 1 = 1
    <if test="status != null">
        AND status = #{status}
    </if>
    <if test="username != null and username != ''">
        AND username = #{username}
    </if>
</select>

上面語句當 status 不為 null 時,以及 username 不為空字符串時,對應(yīng)的查詢條件才會生效。

2. choose、when、otherwise

<choose> 語句類似于 Java 的 switch 語句,可以從多個條件中選擇一個使用。

<select id="selectByUsername" resultType="User">
    SELECT * FROM t_user WHERE 1 = 1
    <choose>
        <when test="status != null">
            AND status = #{status}
        </when>
        <otherwise>
            AND username = #{username}
        </otherwise>
    </choose>
</select>

上面語句根據(jù) status 是否為 null 而執(zhí)行不同的查詢條件,每次查詢只存在一個查詢條件。

3. trim、where、set

在前面的例子中,where 后面都會加上 1=1 這個條件,為了解決當所有條件都沒生效時,where 條件為空而產(chǎn)生異常的問題。

在更新語句 set 后面也會出現(xiàn)同樣的問題,可以使用 <trim> 語句通過指定前綴和后綴的方式來解決。

MyBatis 也提供更簡單的 <where> 語句和 <set> 語句來解決這個問題。

例如下面兩個接口:

public interface UserSqlMapper {
    List<User> selectByUsername(@Param("username") String username, @Param("status") Integer status);
    void updateById(User user);
}

對應(yīng)的 <trim> 映射 where 語句如下:

<select id="selectByUsername" resultType="User">
    SELECT * FROM t_user
    <trim prefix="WHERE" prefixOverrides="AND|OR">
        <if test="status != null">
            AND status = #{status}
        </if>
        <if test="username != null and username != ''">
            AND username = #{username}
        </if>
    </trim>
</select>

對應(yīng)的 <where> 映射語句如下:

<select id="selectByUsername" resultType="User">
    SELECT * FROM t_user
    <where>
        <if test="status != null">
            AND status = #{status}
        </if>
        <if test="username != null and username != ''">
            AND username = #{username}
        </if>
    </where>
</select>

對應(yīng)的 <trim> 映射 update 語句如下:

<update id="updateById">
    UPDATE t_user
    <trim prefix="SET" suffixOverrides=",">
        <if test="status != null">
            status = #{status},
        </if>
        <if test="username != null and username != ''">
            username = #{username},
        </if>
    </trim>
</update>

對應(yīng)的 <set> 映射語句如下:

<update id="updateById">
    UPDATE t_user
    <set>
        <if test="status != null">
            status = #{status},
        </if>
        <if test="username != null and username != ''">
            username = #{username},
        </if>
    </set>
</update>

4. foreach

<foreach> 可以對集合進行遍歷,例如使用 IN 條件語句的時候。

查詢數(shù)據(jù)接口如下:

public interface UserSqlMapper {
    List<User> selectInIdList(@Param("idList") List<Long> idList);
}

對應(yīng)映射語句如下:

<select id="selectInIdList" resultType="User">
    SELECT * FROM t_user
    <where>
        <foreach item="item" index="index" collection="idList" open="id IN (" separator="," close=")" nullable="true">
            #{item}
        </foreach>
    </where>
</select>

item 指定元素,index 指定索引。

5. script

<script> 可以在接口上的注解中使用動態(tài)SQL。

例如查詢數(shù)據(jù)接口如下:

public interface UserSqlMapper {
    @Select("<script>" +
            "SELECT * FROM t_user" +
            "<where>" +
            "   <if test='status != null'>AND status = #{status}</if>" +
            "   <if test='username != null'>AND username = #{username}</if>" +
            "</where>" +
            "</script>")
    List<User> selectByUser(User user);
}

<script> 標簽里面的內(nèi)容和在 XML 文件中的動態(tài) SQL 語法一致。

到此這篇關(guān)于Java中MyBatis的動態(tài)語句詳解的文章就介紹到這了,更多相關(guān)MyBatis的動態(tài)語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論