mybatis中使用not?in與?in的寫法說明
使用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)文章
Java網(wǎng)絡(luò)編程之URL+URLconnection使用方法示例
這篇文章主要介紹了Java網(wǎng)絡(luò)編程之URL+URLconnection使用方法示例,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-11-11
SpringBoot中異常處理實(shí)戰(zhàn)記錄
在我們實(shí)際項(xiàng)目開放中經(jīng)常需要我們處理很多的異常,如何在spring boot項(xiàng)目里面實(shí)現(xiàn)異常處理呢,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中異常處理的相關(guān)資料,需要的朋友可以參考下2022-05-05
Java動(dòng)態(tài)代理Proxy應(yīng)用和底層源碼詳細(xì)分析
Java動(dòng)態(tài)代理是一種在運(yùn)行時(shí)生成代理類的機(jī)制,用于代替手動(dòng)編寫代理類的過程,這篇文章主要給大家介紹了關(guān)于Java動(dòng)態(tài)代理Proxy應(yīng)用和底層源碼詳細(xì)分析的相關(guān)資料,需要的朋友可以參考下2024-03-03
Spring Boot整合EasyExcel(完整版包含上傳解析excel和下載模板)
這篇文章主要介紹了Spring Boot整合EasyExcel(完整版包含上傳解析excel和下載模板),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié)
本文主要介紹了springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
PowerShell用戶認(rèn)證Function實(shí)例代碼
這篇文章主要介紹了PowerShell用戶認(rèn)證Function的資料,并附實(shí)例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09
feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案
這篇文章主要介紹了feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Struts2學(xué)習(xí)筆記(6)-簡單的數(shù)據(jù)校驗(yàn)
這篇文章主要介紹Struts2中的數(shù)據(jù)校驗(yàn),通過一個(gè)簡單的例子來說明,希望能給大家做一個(gè)參考。2016-06-06

