Mybatis使用IN語句查詢的實(shí)現(xiàn)
一、簡介
在SQL語法中如果我們想使用in的話直接可以像如下一樣使用:
select * from HealthCoupon where useType in ( '4' , '3' )
但是如果在MyBatis中的使用in的話,像如下去做的話,肯定會報(bào)錯:
Map<String, Object> selectByUserId(@Param("useType") String useType)
<select id="selectByUserId" resultMap="BaseResultMap" parameterType="java.lang.String">
select * from HealthCoupon where useType in (#{useType,jdbcType=VARCHAR})
</select>
其中useType="2,3";這樣的寫法,看似很簡單,但是MyBatis不支持。。但是MyBatis中提供了foreach語句實(shí)現(xiàn)IN查詢,foreach語法如下:
foreach語句中, collection屬性的參數(shù)類型可以使:List、數(shù)組、map集合
- collection: 必須跟mapper.java中@Param標(biāo)簽指定的元素名一樣
- item: 表示在迭代過程中每一個(gè)元素的別名,可以隨便起名,但是必須跟元素中的#{}里面的名稱一樣。
- index:表示在迭代過程中每次迭代到的位置(下標(biāo))
- open:前綴, sql語句中集合都必須用小括號()括起來
- close:后綴
- separator:分隔符,表示迭代時(shí)每個(gè)元素之間以什么分隔
正確的寫法有以下幾種寫法:
(一)、selectByIdSet(List idList)
如果參數(shù)的類型是List, 則在使用時(shí),collection屬性要必須指定為 list
List<User> selectByIdSet(List idList);
<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE id IN
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
(二)、List<User> selectByIdSet(String[] idList)
如果參數(shù)的類型是Array,則在使用時(shí),collection屬性要必須指定為 array
List<User> selectByIdSet(String[] idList);
<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE id IN
<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
(三)、參數(shù)有多個(gè)時(shí)
當(dāng)查詢的參數(shù)有多個(gè)時(shí),有兩種方式可以實(shí)現(xiàn),一種是使用@Param("xxx")進(jìn)行參數(shù)綁定,另一種可以通過Map來傳參數(shù)。
3.1 @Param("xxx")方式
List<User> selectByIdSet(@Param("name")String name, @Param("ids")String[] idList);
<select id="selectByIdSet" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
from t_user
WHERE name=#{name,jdbcType=VARCHAR} and id IN
<foreach collection="idList" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</select>
3.2 Map方式
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("idList", ids);
mapper.selectByIdSet(params);
<select id="selectByIdSet" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user where
name = #{name}
and ID in
<foreach item="item" index="index" collection="idList" open="(" separator="," close=")">
#{item}
</foreach>
</select>
到此這篇關(guān)于Mybatis使用IN語句查詢的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis IN語句查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cache整合Redis實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring Cache整合Redis實(shí)現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Mybatis如何傳入多個(gè)參數(shù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Mybatis如何傳入多個(gè)參數(shù)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
springboot整合swagger3報(bào)Unable to infer base&nbs
這篇文章主要介紹了springboot整合swagger3報(bào)Unable to infer base url錯誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
InputStreamReader和BufferedReader用法及實(shí)例講解
這篇文章主要介紹了InputStreamReader和BufferedReader用法及實(shí)例講解的相關(guān)資料,需要的朋友可以參考下2015-12-12
Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份
隨著我們的長期使用,Jenkins系統(tǒng)中的內(nèi)容會越來越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個(gè)時(shí)候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份2021-06-06
Java中遍歷Map的多種方法示例及優(yōu)缺點(diǎn)總結(jié)
在java中遍歷Map有不少的方法,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷Map的多種方法,以及各種方法的優(yōu)缺點(diǎn)總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
java實(shí)現(xiàn)簡單汽車租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01

