mybatis foreach循环list的示例分析

发布时间:2022-03-23 11:23:21 作者:小新
来源:亿速云 阅读:605

这篇文章主要为大家展示了“mybatis foreach循环list的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mybatis foreach循环list的示例分析”这篇文章吧。

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循环如下

mybatis foreach循环list的示例分析

这里只做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 foreach循环list的示例分析

原理就是 先告诉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>

以上是“mybatis foreach循环list的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 中断forEach循环的方法
  2. C#中for循环与foreach循环的对比示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis foreach list

上一篇:基于jQuery如何模拟实现淘宝购物车模块

下一篇:Java四个线程常用函数是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》