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

Java中MyBatis的動(dòng)態(tài)語(yǔ)句詳解

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

一、 概述

動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一,通過(guò)不同參數(shù)生成不同的 SQL,可以動(dòng)態(tài)地對(duì)數(shù)據(jù)持久層進(jìn)行操作,而不需要每個(gè)數(shù)據(jù)訪問(wèn)操作都要進(jìn)行手動(dòng)地拼接 SQL 語(yǔ)句。

二、動(dòng)態(tài)語(yǔ)句

1. if

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

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

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

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

<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>

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

2. choose、when、otherwise

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

<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>

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

3. trim、where、set

在前面的例子中,where 后面都會(huì)加上 1=1 這個(gè)條件,為了解決當(dāng)所有條件都沒(méi)生效時(shí),where 條件為空而產(chǎn)生異常的問(wèn)題。

在更新語(yǔ)句 set 后面也會(huì)出現(xiàn)同樣的問(wèn)題,可以使用 <trim> 語(yǔ)句通過(guò)指定前綴和后綴的方式來(lái)解決。

MyBatis 也提供更簡(jiǎn)單的 <where> 語(yǔ)句和 <set> 語(yǔ)句來(lái)解決這個(gè)問(wèn)題。

例如下面兩個(gè)接口:

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

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

<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>

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

<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>

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

<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>

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

<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> 可以對(duì)集合進(jìn)行遍歷,例如使用 IN 條件語(yǔ)句的時(shí)候。

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

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

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

<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> 可以在接口上的注解中使用動(dòng)態(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> 標(biāo)簽里面的內(nèi)容和在 XML 文件中的動(dòng)態(tài) SQL 語(yǔ)法一致。

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

相關(guān)文章

最新評(píng)論