MyBatis動態(tài)SQL表達式詳解
動態(tài) sql 簡單來講就是我們能通過條件的設(shè)置生成不同的 sql,MyBatis 中常用的動態(tài) sql 表達式主要是有五種:
- if
- choose (when, otherwise)
- trim, where, set
- foreach
- sql
if
動態(tài) sql 中最常見的場景是根據(jù)條件查詢,比如要實現(xiàn)一個查詢語句,當(dāng)不傳任何參數(shù)時查詢所有用戶,當(dāng)傳了 id 或者 name 時根據(jù)條件進行查詢,就可以這樣寫:
<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>
對應(yīng)的 mapper 接口如下:
public interface UserMapper {
List<User> selectUserByMap(Map<String,Object> param);
void updateUser(Map<String,Object> param);
}
對應(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 中什么都不傳時,sql 語句變?yōu)椋?/p>
select * from user where 1=1
當(dāng)傳了 id 或 name 時,分別執(zhí)行對應(yīng)的查詢。
choose when otherwise
choose 的使用很像 Java 中的 switch 語法,當(dāng)滿足第一個 when 條件時,就不去看后續(xù)的條件,如果條件都不滿足,則執(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
還記得前面的語法中都寫了 where 1=1 嗎,至于為什么這樣寫,目的在于讓語法能順利執(zhí)行,以 if 語句為例:如果不寫 1=1,語法就會變成下面這樣:
<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>
這個時候如果滿足了第一個 if 條件,那不會有問題,但是如果只滿足第二個條件,語句就會變成:
select * from user where and name=?
語法直接報錯。
MyBatis 提供了一種標(biāo)簽來代替 1=1 的寫法,where 標(biāo)簽只會在子元素返回任何內(nèi)容的情況下才插入 “WHERE” 子句。而且,若子句的開頭為 “AND” 或 “OR”,where 元素也會將它們?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 的語法和 where 類似,在更新時會用到 set:
<update id="updateUser" parameterType="map">
update user
<set>
<if test="name != null">name =#{name},</if>
</set>
where id = #{id}
</update>
使用 where 時會用自動去替換掉 and 或者 or,而使用 set 時會動態(tài)地在行首插入 SET 關(guān)鍵字,并會刪掉額外的逗號。
使用 trim 可以用于去除拼接 sql 時的 and、or 關(guān)鍵字或者逗號等等。
之前使用 where 標(biāo)簽去除了最開始的 and 關(guān)鍵字,用 trim 也同樣可以實現(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 在需要對集合進行遍歷的場景中使用很廣泛,尤其是在 in 語句中,比如下面這條語句:
select * from user where id in (1,2,3,4)
通過 foreach 實現(xiàn)方式如下,foreach 中的 ids 可以是參數(shù)傳入的一個 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片段
通過 sql 片段標(biāo)簽,可以將重復(fù)的 sql 提取出來,使用時通過 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動態(tài)SQL表達式詳解的文章就介紹到這了,更多相關(guān)MyBatis動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java線性結(jié)構(gòu)中的雙向鏈表實現(xiàn)原理
這篇文章將給大家詳細講解雙向鏈表的內(nèi)容,尤其是會通過代碼來進行鏈表的操作,文中的代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-07-07
一文搞懂如何實現(xiàn)Java,Spring動態(tài)啟停定時任務(wù)
定時任務(wù)的應(yīng)用場景十分廣泛,如定時清理文件、定時生成報表、定時數(shù)據(jù)同步備份等。本文將教你實現(xiàn)Java、Spring動態(tài)啟停定時任務(wù),感興趣的可以學(xué)習(xí)一下2022-06-06
java解析php函數(shù)json_encode unicode 編碼問題
這篇文章主要介紹了java解析php函數(shù)json_encode unicode 編碼問題,需要的朋友可以參考下2016-04-04

