Java中MyBatis的動態(tài)語句詳解
一、 概述
動態(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)文章
Spring中@RabbitHandler和@RabbitListener的區(qū)別詳析
@RabbitHandler是用于處理消息的方法注解,它與@RabbitListener注解一起使用,這篇文章主要給大家介紹了關(guān)于Spring中@RabbitHandler和@RabbitListener區(qū)別的相關(guān)資料,需要的朋友可以參考下2024-02-02InputStream數(shù)據(jù)結(jié)構(gòu)示例解析
這篇文章主要為大家介紹了InputStream數(shù)據(jù)結(jié)構(gòu)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10springboot 啟動如何修改application.properties的參數(shù)
這篇文章主要介紹了springboot 啟動如何修改application.properties的參數(shù)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08淺談HttpClient、okhttp和RestTemplate的區(qū)別
這篇文章主要介紹了HttpClient、okhttp和RestTemplate的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心的方法
本文主要介紹了SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Java中字符數(shù)組、String類、StringBuffer三者之間相互轉(zhuǎn)換
這篇文章主要介紹了Java中字符數(shù)組、String類、StringBuffer三者之間相互轉(zhuǎn)換,需要的朋友可以參考下2018-05-05SpringBoot項目離線環(huán)境手動構(gòu)建的過程
文章介紹了如何在IntelliJ IDEA中手動創(chuàng)建一個Spring Boot項目,并詳細講解了pom.xml文件的配置和基本項目結(jié)構(gòu)的設(shè)置,感興趣的朋友跟隨小編一起看看吧2025-01-01