mybatis foreach 循环 list(map)实例
作者:zhouixi 时间:2023-11-23 23:39:05
foreach 循环 list(map)
直接上代码:
整体需求就是
1.分页对象里面有map map里面又有数组对象
2.分页对象里面有list 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保存要循环的ID 或是订单号
mybatis.foreach循环如下
这里只做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我要先循环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>
来源:https://www.cnblogs.com/1-Admin/p/8018773.html
标签:foreach,循环,list,map
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot 内部服务调用方式
2023-08-24 00:32:20
![](https://img.aspxhome.com/file/2023/6/58426_0s.png)
Java集合TreeSet用法详解
2023-11-10 22:53:34
![](https://img.aspxhome.com/file/2023/4/59354_0s.jpg)
分析讲解SpringMVC注解配置如何实现
2023-10-30 17:23:58
c# 获取CookieContainer的所有cookies函数代码
2023-06-17 23:11:30
mybatis中<if>标签bool值类型为false判断方法
2023-11-20 11:28:33
Spring中IOC和AOP的深入讲解
2023-11-19 10:20:59
![](https://img.aspxhome.com/file/2023/9/59529_0s.jpg)
Java编程实现帕斯卡三角形代码示例
2023-11-02 08:08:24
![](https://img.aspxhome.com/file/2023/3/58303_0s.gif)
java控制台打印本月的日历
2023-10-15 22:58:12
![](https://img.aspxhome.com/file/2023/1/58621_0s.jpg)
Java内存模型详解
2023-06-21 21:04:29
![](https://img.aspxhome.com/file/2023/2/57592_0s.webp)
Spring中SmartLifecycle和Lifecycle的作用和区别
2023-11-18 22:55:50
ThreadLocal的set方法原理示例解析
2023-11-09 15:06:09
![](https://img.aspxhome.com/file/2023/1/59181_0s.png)
C语言程序设计50例(经典收藏)
2023-07-10 08:33:19
基于C语言实现静态通讯录的示例代码
2023-07-02 22:07:38
![](https://img.aspxhome.com/file/2023/2/103102_0s.gif)
java 中 poi解析Excel文件版本问题解决办法
2023-11-15 16:49:45
Java关于IO流的全面介绍
2023-08-12 08:14:46
![](https://img.aspxhome.com/file/2023/9/58069_0s.png)
实现java简单的线程池
2023-08-09 06:05:15
![](https://img.aspxhome.com/file/2023/5/58065_0s.png)
实例讲解Java读取一般文本文件和word文档的方法
2023-11-13 05:09:53
synchronized背后的monitor锁实现详解
2023-07-31 08:14:10
Java面试题冲刺第二十五天--并发编程3
2023-09-11 04:40:10
![](https://img.aspxhome.com/file/2023/9/58299_0s.png)
SpringBoot使用Sharding-JDBC实现数据分片和读写分离的方法
2023-07-27 02:15:50
![](https://img.aspxhome.com/file/2023/7/57827_0s.jpg)