Java中MyBatis的動態(tài)語句詳解
一、 概述
動態(tài) SQL 是 MyBatis 的強大特性之一,通過不同參數生成不同的 SQL,可以動態(tài)地對數據持久層進行操作,而不需要每個數據訪問操作都要進行手動地拼接 SQL 語句。
二、動態(tài)語句
1. if
<if>
語句可以根據條件指定 SQL 部分,例如 where
條件。
查詢數據接口如下:
public interface UserSqlMapper { List<User> selectByUsername(@Param("username") String username, @Param("status") Integer status); }
對應映射語句如下:
<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
不為空字符串時,對應的查詢條件才會生效。
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>
上面語句根據 status
是否為 null
而執(zhí)行不同的查詢條件,每次查詢只存在一個查詢條件。
3. trim、where、set
在前面的例子中,where
后面都會加上 1=1
這個條件,為了解決當所有條件都沒生效時,where
條件為空而產生異常的問題。
在更新語句 set
后面也會出現同樣的問題,可以使用 <trim>
語句通過指定前綴和后綴的方式來解決。
MyBatis 也提供更簡單的 <where>
語句和 <set>
語句來解決這個問題。
例如下面兩個接口:
public interface UserSqlMapper { List<User> selectByUsername(@Param("username") String username, @Param("status") Integer status); void updateById(User user); }
對應的 <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>
對應的 <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>
對應的 <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>
對應的 <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
條件語句的時候。
查詢數據接口如下:
public interface UserSqlMapper { List<User> selectInIdList(@Param("idList") List<Long> idList); }
對應映射語句如下:
<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。
例如查詢數據接口如下:
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>
標簽里面的內容和在 XML 文件中的動態(tài) SQL 語法一致。
到此這篇關于Java中MyBatis的動態(tài)語句詳解的文章就介紹到這了,更多相關MyBatis的動態(tài)語句內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中@RabbitHandler和@RabbitListener的區(qū)別詳析
@RabbitHandler是用于處理消息的方法注解,它與@RabbitListener注解一起使用,這篇文章主要給大家介紹了關于Spring中@RabbitHandler和@RabbitListener區(qū)別的相關資料,需要的朋友可以參考下2024-02-02springboot 啟動如何修改application.properties的參數
這篇文章主要介紹了springboot 啟動如何修改application.properties的參數方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08淺談HttpClient、okhttp和RestTemplate的區(qū)別
這篇文章主要介紹了HttpClient、okhttp和RestTemplate的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心的方法
本文主要介紹了SpringBoot項目改為SpringCloud項目使用nacos作為注冊中心,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Java中字符數組、String類、StringBuffer三者之間相互轉換
這篇文章主要介紹了Java中字符數組、String類、StringBuffer三者之間相互轉換,需要的朋友可以參考下2018-05-05