MyBatis動(dòng)態(tài)SQL表達(dá)式詳解
動(dòng)態(tài) sql 簡(jiǎn)單來(lái)講就是我們能通過(guò)條件的設(shè)置生成不同的 sql,MyBatis 中常用的動(dòng)態(tài) sql 表達(dá)式主要是有五種:
- if
- choose (when, otherwise)
- trim, where, set
- foreach
- sql
if
動(dòng)態(tài) sql 中最常見(jiàn)的場(chǎng)景是根據(jù)條件查詢(xún),比如要實(shí)現(xiàn)一個(gè)查詢(xún)語(yǔ)句,當(dāng)不傳任何參數(shù)時(shí)查詢(xún)所有用戶(hù),當(dāng)傳了 id 或者 name 時(shí)根據(jù)條件進(jìn)行查詢(xún),就可以這樣寫(xiě):
<select id="selectUserByMap" parameterType="map" resultType="user"> select * from user where 1=1 <if test="id != null and id!=''"> and id=#{id} </if> <if test="name != null and name!=''"> and name=#{name} </if> </select>
對(duì)應(yīng)的 mapper 接口如下:
public interface UserMapper { List<User> selectUserByMap(Map<String,Object> param); void updateUser(Map<String,Object> param); }
對(duì)應(yīng)的 Java 代碼如下:
Map<String,String> map=new HashMap<String, String>(); map.put("id","1"); map.put("name","javayz"); List<User> users = mapper.selectUserByMap(map);
當(dāng) map 中什么都不傳時(shí),sql 語(yǔ)句變?yōu)椋?/p>
select * from user where 1=1
當(dāng)傳了 id 或 name 時(shí),分別執(zhí)行對(duì)應(yīng)的查詢(xún)。
choose when otherwise
choose 的使用很像 Java 中的 switch 語(yǔ)法,當(dāng)滿(mǎn)足第一個(gè) when 條件時(shí),就不去看后續(xù)的條件,如果條件都不滿(mǎn)足,則執(zhí)行 otherwise:
<select id="selectUserByChoose" parameterType="map" resultType="user"> select * from user where 1=1 <choose> <when test="id != null"> and id=#{id} </when> <when test="name != null"> and name=#{name} </when> <otherwise> and 1=1 </otherwise> </choose> </select>
trim where set
還記得前面的語(yǔ)法中都寫(xiě)了 where 1=1 嗎,至于為什么這樣寫(xiě),目的在于讓語(yǔ)法能順利執(zhí)行,以 if 語(yǔ)句為例:如果不寫(xiě) 1=1,語(yǔ)法就會(huì)變成下面這樣:
<select id="selectUserByMap" parameterType="map" resultType="user"> select * from user where <if test="id != null"> id=#{id} </if> <if test="name != null"> and name=#{name} </if> </select>
這個(gè)時(shí)候如果滿(mǎn)足了第一個(gè) if 條件,那不會(huì)有問(wèn)題,但是如果只滿(mǎn)足第二個(gè)條件,語(yǔ)句就會(huì)變成:
select * from user where and name=?
語(yǔ)法直接報(bào)錯(cuò)。
MyBatis 提供了一種標(biāo)簽來(lái)代替 1=1 的寫(xiě)法,where 標(biāo)簽只會(huì)在子元素返回任何內(nèi)容的情況下才插入 “WHERE” 子句。而且,若子句的開(kāi)頭為 “AND” 或 “OR”,where 元素也會(huì)將它們?nèi)コ?/p>
<select id="selectUserByMap" parameterType="map" resultType="user"> select * from user <where> <if test="id != null"> and id=#{id} </if> <if test="name != null"> and name=#{name} </if> </where> </select>
set 的語(yǔ)法和 where 類(lèi)似,在更新時(shí)會(huì)用到 set:
<update id="updateUser" parameterType="map"> update user <set> <if test="name != null">name =#{name},</if> </set> where id = #{id} </update>
使用 where 時(shí)會(huì)用自動(dòng)去替換掉 and 或者 or,而使用 set 時(shí)會(huì)動(dòng)態(tài)地在行首插入 SET 關(guān)鍵字,并會(huì)刪掉額外的逗號(hào)。
使用 trim 可以用于去除拼接 sql 時(shí)的 and、or 關(guān)鍵字或者逗號(hào)等等。
之前使用 where 標(biāo)簽去除了最開(kāi)始的 and 關(guān)鍵字,用 trim 也同樣可以實(shí)現(xiàn):
<select id="selectUserByMap" parameterType="map" resultType="map"> select * from user <trim prefix="where" prefixOverrides="and"> <if test="id != null"> and id=#{id} </if> <if test="name != null"> and name=#{name} </if> </trim> </select>
foreach
foreach 在需要對(duì)集合進(jìn)行遍歷的場(chǎng)景中使用很廣泛,尤其是在 in 語(yǔ)句中,比如下面這條語(yǔ)句:
select * from user where id in (1,2,3,4)
通過(guò) foreach 實(shí)現(xiàn)方式如下,foreach 中的 ids 可以是參數(shù)傳入的一個(gè) List 集合:
<select id="selectUserByForeach" parameterType="map" resultType="User"> select * from user where id in <foreach collection="ids" item="id" index="index" open="(" separator="," close=")"> #{id} </foreach> </select>
sql片段
通過(guò) sql 片段標(biāo)簽,可以將重復(fù)的 sql 提取出來(lái),使用時(shí)通過(guò) include 引用即可。
<sql id="select_user_where"> <if test="id != null and id!=''"> and id=#{id} </if> <if test="name != null and name!=''"> and name=#{name} </if> </sql> <select id="selectUserByMap" parameterType="map" resultType="user"> select * from user where 1=1 <include refid="select_user_where"> </select>
到此這篇關(guān)于MyBatis動(dòng)態(tài)SQL表達(dá)式詳解的文章就介紹到這了,更多相關(guān)MyBatis動(dòng)態(tài)SQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis動(dòng)態(tài)SQL特性詳解
- Mybatis多表查詢(xún)與動(dòng)態(tài)SQL特性詳解
- MyBatis動(dòng)態(tài)sql查詢(xún)及多參數(shù)查詢(xún)方式
- Mybatis的動(dòng)態(tài)Sql組合模式詳情
- Mybatis如何實(shí)現(xiàn)@Select等注解動(dòng)態(tài)組合SQL語(yǔ)句
- MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫(xiě)方式
- MyBatis在注解上使用動(dòng)態(tài)SQL方式(@select使用if)
- Mybatis中xml的動(dòng)態(tài)sql實(shí)現(xiàn)示例
相關(guān)文章
Java線性結(jié)構(gòu)中的雙向鏈表實(shí)現(xiàn)原理
這篇文章將給大家詳細(xì)講解雙向鏈表的內(nèi)容,尤其是會(huì)通過(guò)代碼來(lái)進(jìn)行鏈表的操作,文中的代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Idea安裝及涉及springboot詳細(xì)配置的圖文教程
這篇文章主要介紹了Idea安裝及涉及springboot詳細(xì)配置,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10JDK版本管理工具jEnv解決不同jdk版本項(xiàng)目
本文主要介紹了JDK版本管理工具jEnv解決不同jdk版本項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07spring boot自定義log4j2日志文件的實(shí)例講解
下面小編就為大家分享一篇spring boot自定義log4j2日志文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11一文搞懂如何實(shí)現(xiàn)Java,Spring動(dòng)態(tài)啟停定時(shí)任務(wù)
定時(shí)任務(wù)的應(yīng)用場(chǎng)景十分廣泛,如定時(shí)清理文件、定時(shí)生成報(bào)表、定時(shí)數(shù)據(jù)同步備份等。本文將教你實(shí)現(xiàn)Java、Spring動(dòng)態(tài)啟停定時(shí)任務(wù),感興趣的可以學(xué)習(xí)一下2022-06-06java解析php函數(shù)json_encode unicode 編碼問(wèn)題
這篇文章主要介紹了java解析php函數(shù)json_encode unicode 編碼問(wèn)題,需要的朋友可以參考下2016-04-04