mybatis foreach 循環(huán) list(map)實例
foreach 循環(huán) list(map)
直接上代碼:
整體需求就是
1.分頁對象里面有map map里面又有數組對象
2.分頁對象里面有l(wèi)ist list里面有map map里面有數組對象。
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(); 實體分頁對象(包括其他頁面屬性)
maps.put("str", str); ? 批量查詢的ID
page.setMaps(maps); ? ? maps對象保存在分頁屬性中
List<Map> mapTest = userService.mapTest(page);
System.out.println(mapTest);需求。請求前臺頁面的時候 需要傳多個訂單號比如1,2
然而其他參數也要有。就要用到分頁實體 跟map結合 分頁實體保存其他屬性。map保存要循環(huán)的ID 或是訂單號
mybatis.foreach循環(huán)如下

這里只做ID或是訂單ID的演示,普通屬性#{id}就行。
取page.maps.str(str是一個數組)
在collection 這里面直接寫 入參.maps
如果入參是LIST
稍微改一下即可
源數據
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
參數的數據結構是一個ArrayList<Map<String, Integer>>,需要以String,Integer為條件批量更新數據庫的數據.
將參數封裝到叫做JsonData的qv中,JsonData的關鍵代碼是
? ? 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>以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java用Arrays.asList初始化ArrayList實例方法
在本篇文章里小編給大家分享的是關于Java中使用Arrays.asList初始化ArrayList的知識點內容,需要的朋友們參考下。2019-10-10
Windows10系統下JDK1.8環(huán)境變量的配置
今天帶大家學習在Windows10系統下怎么配置JDK1.8環(huán)境變量,文中有非常詳細的安裝及配置教程,對正在學習的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
Spring?Security?基于URL的權限判斷源碼解析
這篇文章主要介紹了Spring?Security?基于URL的權限判斷問題,我們想要實現自己的基于請求Url的授權只需自定義一個?AccessDecisionManager即可,接下來跟隨小編一起看看實現代碼吧2021-12-12

