服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis foreach 循环 list(map)实例

mybatis foreach 循环 list(map)实例

2022-10-18 13:45zhouixi Java教程

这篇文章主要介绍了mybatis foreach 循环 list(map)实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

foreach 循环 list(map)

直接上代码:

整体需求就是

1.分页对象里面有map map里面又有数组对象

2.分页对象里面有list list里面有map map里面有数组对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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循环如下

mybatis foreach 循环 list(map)实例

这里只做ID或是订单ID的演示,普通属性#{id}就行。

取page.maps.str(str是一个数组)

在collection 这里面直接写    入参.maps

如果入参是LIST

稍微改一下即可

源数据

?
1
2
3
4
maps.put("str", str);
list.add(maps);
List<Map> mapTest = userService.mapTest1(list);
System.out.println(mapTest);
?
1
2
3
4
5
<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 foreach 循环 list(map)实例

原理就是 先告诉mybatis我要先循环list然后拿到list里面的map.str 即可。

使用foreach处理list中的map

参数的数据结构是一个ArrayList<Map<String, Integer>>,需要以String,Integer为条件批量更新数据库的数据.

将参数封装到叫做JsonData的qv中,JsonData的关键代码是

?
1
2
3
4
5
6
7
8
    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中的方法是

?
1
updateXxxx(JsonData jsonData);

Mapper.xml的sql是

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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

延伸 · 阅读

精彩推荐