mybatis中使用not?in與?in的寫法說(shuō)明
使用not in與 in的寫法
首先聲明我不是很喜歡用foreach,所以我的代碼中很少出現(xiàn)foreach。不廢話了,上代碼:
in的用法
我的id是Long類型的
service方法,有一個(gè)Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("ids", idStr);
? ? }實(shí)體類.xml
select * from xxx where?
<if test="ids != null"> FIND_IN_SET(id,#{ids}) ? </if>not in的用法
service方法,有一個(gè)Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("notids", idStr);
? ? }實(shí)體類.xml
select * from xxx where?
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids}) ? </if>使用in查詢時(shí)的注意事項(xiàng)
當(dāng)查詢的參數(shù)只有一個(gè)時(shí)
findByIds(List<Long> ids)
a 如果參數(shù)的類型是List, 則在使用時(shí),collection屬性要必須指定為 list
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ?Select
? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ?from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="list"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>??findByIds(Long[] ids)
b 如果參數(shù)的類型是Array,則在使用時(shí),collection屬性要必須指定為 array
? <select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>當(dāng)查詢的參數(shù)有多個(gè)時(shí)
例如 findByIds(String name, Long[] ids)
這種情況需要特別注意,在傳參數(shù)時(shí),一定要改用Map方式, 這樣在collection屬性可以指定名稱
下面是一個(gè)示例
? ? ? ? ?Map<String, Object> params = new HashMap<String, Object>(2);
? ? ? ? params.put("name", name);
? ? ? ? ?params.put("ids", ids);
? ? ? ? mapper.findByIdsMap(params);
?
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="ids"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? ?</select>?完整的示例如下:
例如有一個(gè)查詢功能,Mapper接口文件定義如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查詢的sql拼裝方法如下:
?<select id="findbyIds" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>?以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis之類型處理器TypeHandler的作用與自定義方式
這篇文章主要介紹了Mybatis之類型處理器TypeHandler的作用與自定義方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
淺談synchronized加鎖this和class的區(qū)別
synchronized 是 Java 語(yǔ)言中處理并發(fā)問(wèn)題的一種常用手段,本文主要介紹了synchronized加鎖this和class的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11
Java客戶端調(diào)用.NET的WebService實(shí)例
下面小編就為大家?guī)?lái)一篇Java客戶端調(diào)用.NET的WebService實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Java 使用Calendar計(jì)算時(shí)間的示例代碼
這篇文章主要介紹了Java 使用Calendar計(jì)算時(shí)間的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java實(shí)現(xiàn)控制臺(tái)輸出兩點(diǎn)間距離
這篇文章主要介紹了Java實(shí)現(xiàn)控制臺(tái)輸出兩點(diǎn)間距離,涉及了部分編程坐標(biāo)的問(wèn)題,具有一定參考價(jià)值,需要的朋友可以了解下2017-09-09
Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法
這篇文章主要為大家介紹了Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,代碼都有詳細(xì)的注釋,感興趣的小伙伴們可以參考一下2016-01-01
SWT(JFace) Wizard(Eclipse插件編程必備)
SWT(JFace)小制作:Wizard(Eclipse插件編程必備)2009-06-06

