Mybatis中foreach的使用詳解
一、foreach 屬性使用
<foreach collection="list" index="index" item="mchntCd" open="(" close=")" separator=","> #{mchntCd} </foreach>
- item: 集合中元素迭代時(shí)的別名,該參數(shù)為必選,通過別名進(jìn)行取值
- index:在list和數(shù)組中,index是元素的序號(hào),在map中,index是元素的key,非必填
- open: foreach代碼的開始符號(hào),一般是(和close=")"合用。常用在in(),values()時(shí)。非必填
- separator:元素之間的分隔符,例如在in()的時(shí)候,separator=","會(huì)自動(dòng)在元素中間用“,“隔開,避免手動(dòng)輸入逗號(hào)導(dǎo)致sql錯(cuò)誤,如in(1,2,)這樣。非必填
- close: foreach代碼的關(guān)閉符號(hào),一般是)和open="("合用。常用在in(),values()時(shí)。非必填
- collection: 要做foreach的對(duì)象,作為入?yún)?ul>
- 傳入是集合,也就是接口里面用的 List<String> nameList 那么 使用 collection = “list”
- 傳入的是數(shù)組,接口里面 String[] namestrs ,那么 使用 collection = “array”
- 如果傳入的是一個(gè)實(shí)體bean,實(shí)體bean里面有一個(gè)屬性為 list<String> ids 那么collection = “ids ”,如果實(shí)體bean中對(duì)應(yīng)ids是一個(gè)對(duì)象,ids 里面有一個(gè)屬性為 List<Stirng> usernames,那么collection = “ids.usernames”
二、代碼使用
1、實(shí)體類 list<String> mchntCds
mapper接口 ---》 list方式
int queryDiscDerateCount(List<String> mchntCds);
mapper映射xml:
<select id="queryDiscDerateCount" resultType="Integer"> select count(*) from t_mchnt_disc_config where mchnt_cd in <foreach collection="list" index="index" item="mchntCd" open="(" close=")" separator=","> #{mchntCd} </foreach> </select>
2、實(shí)體類 list<User> userlist,實(shí)體類中有 list 屬性
實(shí)體類:
@Data public class MchntDiscDerateDto { private String mchntCd = ""; }
mapper接口
List<TMchntDerateInfoDto> getDiscDerateList(List<MchntDiscDerateDto> discDto);
mapper映射xml:
<select id="getDiscDerateList" parameterType="MchntDiscDerateDto" resultType="TMchntDerateInfoDto"> select t_mchnt_disc_derate_config where mchnt_cd in <foreach collection="list" index="index" item="user" open="(" close=")" separator=","> #{user.mchntCd} </foreach> </select>
3、數(shù)組 String[] params
用 array
mapper 接口
int queryDiscDerateCount(String[] mchntCds);
mapper映射xml:
<select id="queryDiscDerateCount" resultType="Integer"> select count(*) from t_mchnt_disc_derate_config where mchnt_cd in <foreach collection="array" index="index" item="mchntCd" open="(" close=")" separator=","> #{mchntCd} </foreach> </select>
4、傳入的參數(shù)是實(shí)體類,并且實(shí)體中包含數(shù)組和集合
實(shí)體類:
@Data public class UserVo { private Long id; private Long supplierId; private Long[] ids; private List<Long> clientIdList; }
mapper接口
List<UserVo> queryList(UserVo select);
mapper映射文件xml
<select id="queryList" resultType="UserVo" parameterType="UserVo"> select * from bms_bills_memo <where> and id in <foreach collection="ids" open="(" close=")" item="id" separator=","> #{id} </foreach> and client_id in <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" > #{detail} </foreach> </where> </select>
5、傳入多個(gè)list或者array,不使用實(shí)體進(jìn)行封裝。用注解@Params, collection使用到Param中定義的別名
mapper接口
List<UserVo> queryList(@Param("idArray") Long[] array, @Param("clientIdList") List<Long> list);
mapper映射文件xml
<select id="queryList" resultType="UserVo"> select * from t_user_inf <where> and id in <foreach collection="idArray" open="(" close=")" item="id" separator=","> #{id} </foreach> and client_id in <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" > #{detail} </foreach> </where> </select>
6、map參數(shù)
當(dāng)我們傳入的參數(shù)為 Map<String,Objject>的時(shí)候,那么能不能正常使用 foreach呢,答案是肯定的,因?yàn)閷?duì)象也是類似于map結(jié)構(gòu)啊
/** * map 獲取數(shù)據(jù) * @param map * @return */ List<SysUser> getUserByIds(Map<String,Object> map);
// xml <select id="getUserByIds" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_sys_user where status = #{status} and id in <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach> </select>
實(shí)際調(diào)用:
@Test public void testMapUsers() { Map<String,Object> params = new HashMap<>(); params.put("status", "1"); params.put("ids", Arrays.asList(1,2,3,4,5,6,7,8)); List<SysUser> all = sysUserDao.getUserByIds(params); try { System.out.println(new JsonMapper().writeValueAsString(all)); } catch (JsonProcessingException e) { e.printStackTrace(); } }
調(diào)用結(jié)果:
三、總結(jié)
1、mapper 接口中添加 @Param注解的場(chǎng)合,list,array將會(huì)失效;
2、使用了 @Param注解 collection的內(nèi)容需要使用Param中的別名來指定你需要用到的list或者array
擴(kuò)展:Mybatis中foreach的使用
首先我們要明白的是foreach的本質(zhì)就是把數(shù)據(jù)庫能執(zhí)行的sql在xml中按照一定語法來進(jìn)行拼接,所以拼接之前,我們了解一下foreach標(biāo)簽中幾個(gè)常見元素的作用
1.collection
List或Array:如果傳入的參數(shù)類型是List或Array,collection屬性的默認(rèn)值分別是list和array。如果需要自定義集合名稱。
Map:如果傳入的參數(shù)是Map,collection屬性可以指定遍歷Map的keys、values或entrySet
2.item
集合遍歷中每一個(gè)元素的別名
3.open
拼接sql時(shí)最前面拼接的字符串
4.separator
拼接sql時(shí)候兩個(gè)元素之間的分隔字符串
5.close
拼接sql時(shí)最后面拼接的字符串
6.index
index:在List或Array中,index為元素的序號(hào)索引;在Map中,index為遍歷元素的key值。
舉一個(gè)簡(jiǎn)單的例子
一個(gè)簡(jiǎn)單的sql
select * from blog where title is not null and (id=1 or id=2 or id=3)
1.我們使用map集合作為參數(shù)實(shí)現(xiàn)拼接
<select id="queryBlogForeach" parameterType="map" resultType="blog"> select * from blog <where> title is not null <foreach collection="ids" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>
2.我們使用list集合作為參數(shù)實(shí)現(xiàn)拼接
<select id="queryBlogForeach2" parameterType="list" resultType="blog"> select * from blog <where> title is not null <foreach collection="list" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>
到此這篇關(guān)于Mybatis中foreach的使用的文章就介紹到這了,更多相關(guān)Mybatis foreach使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring實(shí)戰(zhàn)之抽象Bean和子Bean定義與用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之抽象Bean和子Bean定義與用法,結(jié)合實(shí)例形式分析了Spring抽象Bean和子Bean相關(guān)配置、定義與使用操作技巧,需要的朋友可以參考下2019-11-11Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng)
今天小編就為大家分享一篇關(guān)于Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03使用SpringBoot簡(jiǎn)單實(shí)現(xiàn)一個(gè)蘋果支付的場(chǎng)景
這篇文章主要為大家詳細(xì)介紹了如何在Spring?Boot項(xiàng)目中集成Apple?Pay功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11Java中的system.getProperty()的作用及使用方法
System.getProperty()?方法用于獲取系統(tǒng)屬性的值,該方法接受一個(gè)字符串參數(shù),表示要獲取的系統(tǒng)屬性的名稱,返回值為字符串類型,表示該屬性的值,接下來通過本文給大家介紹Java中的system.getProperty()的作用及使用方法,感興趣的朋友跟隨小編一起看看吧2023-05-05Java Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解
這篇文章主要介紹了Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07