mybatis foreach 循環(huán) list(map)實(shí)例
foreach 循環(huán) list(map)
直接上代碼:
整體需求就是
1.分頁(yè)對(duì)象里面有map map里面又有數(shù)組對(duì)象
2.分頁(yè)對(duì)象里面有l(wèi)ist list里面有map map里面有數(shù)組對(duì)象。
public class Page {
private Map maps;
private List lists;
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
}
String [] str = {"1,2"};
Page page = new Page(); 實(shí)體分頁(yè)對(duì)象(包括其他頁(yè)面屬性)
maps.put("str", str); ? 批量查詢的ID
page.setMaps(maps); ? ? maps對(duì)象保存在分頁(yè)屬性中
List<Map> mapTest = userService.mapTest(page);
System.out.println(mapTest);需求。請(qǐng)求前臺(tái)頁(yè)面的時(shí)候 需要傳多個(gè)訂單號(hào)比如1,2
然而其他參數(shù)也要有。就要用到分頁(yè)實(shí)體 跟map結(jié)合 分頁(yè)實(shí)體保存其他屬性。map保存要循環(huán)的ID 或是訂單號(hào)
mybatis.foreach循環(huán)如下

這里只做ID或是訂單ID的演示,普通屬性#{id}就行。
取page.maps.str(str是一個(gè)數(shù)組)
在collection 這里面直接寫(xiě) 入?yún)?maps
如果入?yún)⑹荓IST
稍微改一下即可
源數(shù)據(jù)
maps.put("str", str);
list.add(maps);
List<Map> mapTest = userService.mapTest1(list);
System.out.println(mapTest);<foreach item="items" index="index" collection="list" open="(" ?separator="," ?close=")"> -->
? ? ? <foreach item="item" index="index" collection="items.str" open="(" ?separator="," ?close=")" ? >
? ? ? ? ? ? ? ? #{item}
? ? ? </foreach>
</foreach>
原理就是 先告訴mybatis我要先循環(huán)list然后拿到list里面的map.str 即可。
使用foreach處理list中的map
參數(shù)的數(shù)據(jù)結(jié)構(gòu)是一個(gè)ArrayList<Map<String, Integer>>,需要以String,Integer為條件批量更新數(shù)據(jù)庫(kù)的數(shù)據(jù).
將參數(shù)封裝到叫做JsonData的qv中,JsonData的關(guān)鍵代碼是
? ? private ArrayList<Map<String, Integer>> usersPlatforms;
? ? public ArrayList<Map<String, Integer>> getUsersPlatforms() {
? ? ? ? return usersPlatforms;
? ? }
?
? ? public void setUsersPlatforms(ArrayList<Map<String, Integer>> usersPlatforms) {
? ? ? ? this.usersPlatforms = usersPlatforms;
? ? }Mapper中的方法是
updateXxxx(JsonData jsonData);
Mapper.xml的sql是
<update id="updateXxxx" parameterType="JsonData">
? ? ? ? UPDATE xxx SET `xx` = 10
? ? ? ? <where>
? ? ? ? ? ? <foreach collection="usersPlatforms" item="userPlatform" open="" close="" separator="OR">
? ? ? ? ? ? ? ? <foreach collection="userPlatform.keys" item="key" open=" user_id = " close="" separator="">
? ? ? ? ? ? ? ? ? ? #{key}
? ? ? ? ? ? ? ? </foreach>
? ? ? ? ? ? ? ? <foreach collection="userPlatform.values" item="value" open=" AND platform = " close="" separator="">
? ? ? ? ? ? ? ? ? ? #{value}
? ? ? ? ? ? ? ? </foreach>
? ? ? ? ? ? </foreach>
? ? ? ? </where>
? ? </update>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java開(kāi)發(fā)ShardingSphere的路由引擎類(lèi)型示例詳解
這篇文章主要為大家介紹了java開(kāi)發(fā)ShardingSphere的路由引擎類(lèi)型示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)指南
之前沒(méi)有接觸過(guò)購(gòu)物車(chē)的東東,也不知道購(gòu)物車(chē)應(yīng)該怎么做,所以在查詢了很多資料,總結(jié)一下購(gòu)物車(chē)的功能實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于JavaWeb購(gòu)物車(chē)項(xiàng)目開(kāi)發(fā)的相關(guān)資料,需要的朋友可以參考下2022-06-06
使用Java實(shí)現(xiàn)系統(tǒng)托盤(pán)功能的介紹(附源碼以及截圖)
本篇文章介紹了,在Java中實(shí)現(xiàn)系統(tǒng)托盤(pán)功能的詳解,文中附源碼以及截圖介紹。需要的朋友參考下2013-05-05
SpringBoot中實(shí)現(xiàn)異步調(diào)用@Async詳解
這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)異步調(diào)用@Async詳解,在SpringBoot的日常開(kāi)發(fā)中,一般都是同步調(diào)用的,但實(shí)際中有很多場(chǎng)景非常適合使用異步來(lái)處理,需要的朋友可以參考下2024-01-01
Java之Spring簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象
這篇文章主要介紹了Spring的讀取和存儲(chǔ)對(duì)象,獲取 bean 對(duì)象也叫做對(duì)象裝配,是把對(duì)象取出來(lái)放到某個(gè)類(lèi)中,有時(shí)候也叫對(duì)象注?,想進(jìn)一步了解的同學(xué)可以參考本文2023-04-04
Java8中常用的日期時(shí)間工具類(lèi)總結(jié)
這篇文章主要為大家詳細(xì)介紹了Java8中常用的三個(gè)日期時(shí)間工具類(lèi),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07
SpringMVC請(qǐng)求的路徑變量里面寫(xiě)正則表達(dá)式的方法
這篇文章主要介紹了SpringMVC請(qǐng)求的路徑變量里面寫(xiě)正則表達(dá)式的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

