mybatis使用foreach处理List中的Map mybatis-----传入传出多个参数,都是map或list,批量更新
https://nannan408.iteye.com/blog/2170470
https://blog.csdn.net/xingzhishen/article/details/86424395
参数的数据结构是一个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>
----------------------------------- ----------------------------------- ----------------------------------- -----------------------------------
1.前言.
如题.
2.代码.
(1)mapper.xml.
- <select id="getTest" resultType="java.util.HashMap" parameterType="java.util.HashMap" >
- select count(1) as c1,userid as c2 from test where insertime <![CDATA[>=]]> #{beginTime,jdbcType=TIMESTAMP} and insertime <![CDATA[<]]> #{endTime,jdbcType=TIMESTAMP} group by userid
- </select>
(2)interface
- public interface TestMapper{
- List<Map<String,Object>> getTest(Map<String,Object> map);
- }
(3)
测试类:
- @Test
- public void test3(){
- SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHH");
- Date d1 = null;
- try {
- d1 = sf.parse("2014061100");
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Date d2 = null;
- try {
- d2 = sf.parse("2014121100");
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //new
- Map map=new HashMap<String, Object>();
- map.put("beginTime", d1);
- map.put("endTime", d2);
- List list=testMapper.getTest(map);
- System.out.println(list.size());
- }
2.批量更新.
大部分传list就可以了,传map也可以,但map也要解析成list,可以自行研究map,这里介绍通用的list传值方法:
(1)mapper
- public int batchUpdate(List<Test> list);
(2)xml
- <update id="batchUpdate" parameterType="java.util.List">
- <foreach collection="list" item="list" index="index" open="begin" close=";end;" separator=";">
- update Test
- <set>
- A= A + #{list.a}
- </set>
- where B = #{list.b}
- </foreach >
- </update>
(3)测试类
- public void testBatchUpdate(){
- List<Test > item=new ArrayList<Test>();
- for(int i=0;i<10;i++){
- Test Test=new Test();
- Test.setA(i+10);
- Test.setB("kkk");
- item.add(Test);
- }
- int count= TestMapper.batchUpdate(item);
- System.out.println("jieguo:"+ count);
- }
mybatis使用foreach处理List中的Map mybatis-----传入传出多个参数,都是map或list,批量更新的更多相关文章
- CenOS中的yum配置文件CentOS-Base.repo里面的参数都是何含义? souhu CentOS-Base.repo
souhu yum服务器CentOS-Base.repo 将$releasever替换为操作系统版本号 # CentOS-Base.repo # # The mirror system uses t ...
- mybatis学习之路----批量更新数据两种方法效率对比
原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...
- js中当call或者apply传入的第一个参数是null/undefined时,js函数内执行的上下文环境是什么?
在js中我们都知道call/apply,还有比较少用的bind;传入的第一个参数都是改变函数当前上下文对象; call/apply区别在于传的参数不同,一个是已逗号分隔字符串,一个以数组形式.而bin ...
- MyBatis的foreach查询(List、Array、Map)
mybatis 中 foreach collection的三种用法 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index ...
- Mybatis之foreach用法----List、Array、Map三种类型遍历
在mybatis的xml文件中构建动态sql语句时,经常会用到标签遍历查询条件.特此记录下不同情况下书写方式!-------仅供大家参考------ 1. foreach元素的属性 collectio ...
- MyBatis的foreach语句详解 list array map
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.it ...
- 关于mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数
一.入参为List的写法: <select id="queryParamList" resultType="map" parameterType=&quo ...
- 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明
<foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...
- eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法
当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...
随机推荐
- mysql(一)工作原理 & 数据库引擎
参考文档 http://www.cnblogs.com/xiaotengyi/articles/3641983.html innodb:https://www.cnblogs.com/Aiapple/ ...
- [BUAA软工]Alpha阶段项目展示
[冰多多]Alpha项目展示 冰多多项目: 语音coding 助手, alpha阶段目标: 语音辅助输入 一. 团队成员的简介和个人博客地址 成员 角色 个人博客地址 卓培锦 PM, 后端开发 htt ...
- VS2015 创建C++动态库及使用
转载:https://blog.csdn.net/w_x_myself/article/details/82252646 1.dll的特点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代 ...
- odoo开发笔记 -- 当前时间&时间运算相关
当前日期/时间: from datetime import datetime, timedelta now_time = datetime.now() today_date_format = now_ ...
- 使用python脚本批量设置nginx站点的rewrite规则
一般情况下,配置rewrite重写规则使用shell脚本即可: 把url拼凑成1,2文件中中的格式,运行 chongxie.sh 即可生成我们需要的rewrite规则 [root@web01:/opt ...
- 编写基于TCP的应用程序
编写基于TCP的应用程序 这似乎是一个非常简单的话题, 就跟"是个人就能做网站"一样, 你可能也认为"是个人就能写使用TCP socket的网络程序". 不 ...
- Heartbeat took longer than "00:00:01" at "09/06/2019 05:08:08 +00:00".
.netcore在k8s+docker+linux,部署后,偶尔会报这样的警告 Warn:Microsoft.AspNetCore.Server.KestrelHeartbeat took longe ...
- 动手制作 java版本切换 多版本JDK安装 windows JDK版本 切换
[参考]windows下JDK版本之间的切换 1.下载各版本安装包,指定安装位置顺序安装 2.删除注册表,文件和环境变量 文件: C:\Windows\System32 下java相关文件如 jav ...
- Python - Django - auth 模块
生成数据库 查看 auth_user 数据库 给 auth_user 表创建一个超级用户 邮箱地址可以不写 再看一下 auth_user 表 密码被加密了 login.html: <!DOCTY ...
- B+树比B树更适合实际应用中操作系统的文件索引和数据库索引
B+树比B树更适合实际应用中操作系统的文件索引和数据库索引 为什么选择B+树作为数据库索引结构? 背景 首先,来谈谈B树.为什么要使用B树?我们需要明白以下两个事实: [事实1]不同容量的存储器, ...