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.

  1. <select id="getTest" resultType="java.util.HashMap" parameterType="java.util.HashMap" >
  2. 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
  3. </select>

(2)interface

  1. public interface TestMapper{
  2. List<Map<String,Object>> getTest(Map<String,Object> map);
  3. }

(3) 
测试类:

  1. @Test
  2. public void test3(){
  3. SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHH");
  4. Date d1 = null;
  5. try {
  6. d1 = sf.parse("2014061100");
  7. } catch (ParseException e) {
  8. // TODO Auto-generated catch block
  9. e.printStackTrace();
  10. }
  11. Date d2 = null;
  12. try {
  13. d2 = sf.parse("2014121100");
  14. } catch (ParseException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18. //new
  19. Map map=new HashMap<String, Object>();
  20. map.put("beginTime", d1);
  21. map.put("endTime", d2);
  22. List list=testMapper.getTest(map);
  23. System.out.println(list.size());
  24. }

2.批量更新. 
   大部分传list就可以了,传map也可以,但map也要解析成list,可以自行研究map,这里介绍通用的list传值方法: 
(1)mapper

  1. public  int batchUpdate(List<Test> list);

(2)xml

  1. <update id="batchUpdate" parameterType="java.util.List">
  2. <foreach collection="list" item="list" index="index" open="begin" close=";end;" separator=";">
  3. update Test
  4. <set>
  5. A= A + #{list.a}
  6. </set>
  7. where B = #{list.b}
  8. </foreach >
  9. </update>

(3)测试类

    1. public void testBatchUpdate(){
    2. List<Test > item=new ArrayList<Test>();
    3. for(int i=0;i<10;i++){
    4. Test Test=new Test();
    5. Test.setA(i+10);
    6. Test.setB("kkk");
    7. item.add(Test);
    8. }
    9. int count=  TestMapper.batchUpdate(item);
    10. System.out.println("jieguo:"+ count);
    11. }

mybatis使用foreach处理List中的Map mybatis-----传入传出多个参数,都是map或list,批量更新的更多相关文章

  1. CenOS中的yum配置文件CentOS-Base.repo里面的参数都是何含义? souhu CentOS-Base.repo

    souhu  yum服务器CentOS-Base.repo 将$releasever替换为操作系统版本号 # CentOS-Base.repo # # The mirror system uses t ...

  2. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  3. js中当call或者apply传入的第一个参数是null/undefined时,js函数内执行的上下文环境是什么?

    在js中我们都知道call/apply,还有比较少用的bind;传入的第一个参数都是改变函数当前上下文对象; call/apply区别在于传的参数不同,一个是已逗号分隔字符串,一个以数组形式.而bin ...

  4. MyBatis的foreach查询(List、Array、Map)

    mybatis 中 foreach collection的三种用法 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index ...

  5. Mybatis之foreach用法----List、Array、Map三种类型遍历

    在mybatis的xml文件中构建动态sql语句时,经常会用到标签遍历查询条件.特此记录下不同情况下书写方式!-------仅供大家参考------ 1. foreach元素的属性 collectio ...

  6. MyBatis的foreach语句详解 list array map

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,open,separator,close.it ...

  7. 关于mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数

    一.入参为List的写法: <select id="queryParamList" resultType="map" parameterType=&quo ...

  8. 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明

    <foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...

  9. eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法

    当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...

随机推荐

  1. Pandas学习整理与实践

    Part 1. Pandas初识 作为一款数据处理工具,Pandas本身集成了Numpy(数据计算处理)及matplotlib(绘图),其便捷的数据处理能力.方便的文件读写以及支持多维度的表示方式使其 ...

  2. js使浏览器窗口最大化(适用于IE的方法)

    这里使用的方法是IE的私有特性,只能在IE中有效.主要是window.moveTo和 window.resizeTo方法.       效果和点击最大化按钮差不多,有一点区别.点击最大化按钮后,浏览器 ...

  3. anchor_based-anchor_free object detectors

    同步到知乎anchor_based-anchor_free object detectors 前言:最近关注了大量目标检测的论文,比较火的就是anchor based和anchor free两类问题: ...

  4. [zhuan]SQLSERVER 数据库性能的基本

    SQLSERVER 数据库性能的基本 很久没有写文章了,在系统正式上线之前,DBA一般都要测试一下服务器的性能 比如你有很多的服务器,有些做web服务器,有些做缓存服务器,有些做文件服务器,有些做数据 ...

  5. Web打印的解决方案之证件套打

    由于以前未接触过套打,一直觉得套打是一个比较神秘和麻烦的事情,因为打印机的位置总是需要调整的,你总不能硬编码吧?但是如果位置可调,有需要直观一些来处理,那就比较麻烦了. 在前面介绍过<Web打印 ...

  6. jQuery-webcam使用

    基本页面 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  7. Windows删除空文件夹问题带来的学习与思考

    https://blog.csdn.net/XavierDarkness/article/details/84785482 1.问题产生:   前段时间得到一份资源,号称有近500个G的学习资料,花了 ...

  8. (.Net) NLog 记录日志功能

    https://codeload.github.com/NLog/NLog/zip/v4.6.6 https://nlog-project.org/?r=redirect Logger logger ...

  9. (转载)人脸识别中Softmax-based Loss的演化史

    人脸识别中Softmax-based Loss的演化史  旷视科技 近期,人脸识别研究领域的主要进展之一集中在了 Softmax Loss 的改进之上:在本文中,旷视研究院(上海)(MEGVII Re ...

  10. ubuntu16.04+cuda8.0+cudnn6.0安装mxnet(极简!+成功!)

    安装MXNet 1.安装 CUDA8.0对应的mxnet版本是mxnet-cu80(同理如果是CUDA9.0对应版本则是mxnet-cu90). 如果pip安装过慢,请参考 Ubuntu16.10下配 ...