zoukankan      html  css  js  c++  java
  • 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. 

    Java代码  收藏代码
    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 

    Java代码  收藏代码
    1. public interface TestMapper{  
    2.   
    3.     List<Map<String,Object>> getTest(Map<String,Object> map);  
    4. }  



    (3) 
    测试类: 

    Java代码  收藏代码
    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 

    Java代码  收藏代码
    1. public  int batchUpdate(List<Test> list);  


    (2)xml 

    Java代码  收藏代码
    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)测试类 

    Java代码  收藏代码
      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.     }  
  • 相关阅读:
    读入输出优化
    【JSOI2008】星球大战 并查集
    堆STL和重载运算符
    树的直径
    H3C三层交换机(S5500)清除配置信息并进行简单配置
    简单的逻辑学
    Java基础--第十八天
    Java基础--第十七天
    Java基础--第十六天
    Java基础--第十五天
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/11199278.html
Copyright © 2011-2022 走看看