您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“MyBatis動態(tài)SQL表達式怎么使用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
動態(tài) sql 簡單來講就是我們能通過條件的設(shè)置生成不同的 sql,MyBatis 中常用的動態(tài) sql 表達式主要是有五種:
if
choose (when, otherwise)
trim, where, set
foreach
sql
動態(tài) sql 中最常見的場景是根據(jù)條件查詢,比如要實現(xiàn)一個查詢語句,當不傳任何參數(shù)時查詢所有用戶,當傳了 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);
當 map 中什么都不傳時,sql 語句變?yōu)椋?/p>
select * from user where 1=1
當傳了 id 或 name 時,分別執(zhí)行對應(yīng)的查詢。
choose 的使用很像 Java 中的 switch 語法,當滿足第一個 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>
還記得前面的語法中都寫了 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 提供了一種標簽來代替 1=1 的寫法,where 標簽只會在子元素返回任何內(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 標簽去除了最開始的 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 在需要對集合進行遍歷的場景中使用很廣泛,尤其是在 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 片段標簽,可以將重復(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>
“MyBatis動態(tài)SQL表達式怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。