mybatis设置update批处理语句

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

20170913 21:58:06

阅读数:27235

点滴记载,点滴进步,愿自己更上一层楼。


 

上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题。

实现方式有两种,

一种用for循环通过循环传过来的参数集合,循环出Nsql

另一种 mysqlcase when 条件判断变相的进行批量更新  

下面进行实现。

注意第一种方法要想成功,需要在db链接url后面带一个参数  &allowMultiQueries=true

即:  jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true

其实这种东西写过来写过去就是差不多一样的代码,不做重复的赘述,直接上代码。

  1.     <!-- 这次用resultmap接收输出结果  -->
  2.     <select id="findByName" parameterType="string" resultMap="customerMap">
  3.         select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100
  4.     </select>
  5.  
  6.  
  7.     <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->
  8.     <update id="batchUpdate" parameterType="java.util.Map">
  9.         <!-- 接收list参数,循环着组装sql语句,注意for循环的写法
  10.              separator=";" 代表着每次循环完,在sql后面放一个分号
  11.              item="cus" 循环List的每条的结果集
  12.              collection="list" list 即为 map传过来的参数key -->
  13.         <foreach collection="list" separator=";" item="cus">
  14.             update t_customer set
  15.             c_name = #{cus.name},
  16.             c_age = #{cus.age},
  17.             c_sex = #{cus.sex},
  18.             c_ceroNo = #{cus.ceroNo},
  19.             c_ceroType = #{cus.ceroType}
  20.             where id = #{cus.id}
  21.         </foreach>
  22.     </update>
  23.  
  24.     <!-- 批量更新第二种方法,通过 case when语句变相的进行批量更新 -->
  25.     <update id="batchUpdateCaseWhen" parameterType="java.util.Map">
  26.         update t_customer
  27.         <trim prefix="set" suffixOverrides=",">
  28.             <!-- 拼接case when 这是一种写法 -->
  29.             <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
  30.             <!--when #{cus.id} then #{cus.age}-->
  31.             <!--</foreach>-->
  32.  
  33.             <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->
  34.             <trim prefix="c_name =case" suffix="end,">
  35.                 <foreach collection="list" item="cus">
  36.                     <if test="cus.name!=null">
  37.                         when id=#{cus.id} then #{cus.name}
  38.                     </if>
  39.                 </foreach>
  40.             </trim>
  41.             <trim prefix="c_age =case" suffix="end,">
  42.                 <foreach collection="list" item="cus">
  43.                     <if test="cus.age!=null">
  44.                         when id=#{cus.id} then #{cus.age}
  45.                     </if>
  46.                 </foreach>
  47.             </trim>
  48.             <trim prefix="c_sex =case" suffix="end,">
  49.                 <foreach collection="list" item="cus">
  50.                     <if test="cus.sex!=null">
  51.                         when id=#{cus.id} then #{cus.sex}
  52.                     </if>
  53.                 </foreach>
  54.             </trim>
  55.             <trim prefix="c_ceroNo =case" suffix="end,">
  56.                 <foreach collection="list" item="cus">
  57.                     <if test="cus.ceroNo!=null">
  58.                         when id=#{cus.id} then #{cus.ceroNo}
  59.                     </if>
  60.                 </foreach>
  61.             </trim>
  62.             <trim prefix="c_ceroType =case" suffix="end,">
  63.                 <foreach collection="list" item="cus">
  64.                     <if test="cus.ceroType!=null">
  65.                         when id=#{cus.id} then #{cus.ceroType}
  66.                     </if>
  67.                 </foreach>
  68.             </trim>
  69.         </trim>
  70.         <where>
  71.             <foreach collection="list" separator="or" item="cus">
  72.                 id = #{cus.id}
  73.             </foreach>
  74.         </where>
  75.     </update>



 

 

接口

  1.     List<Customer> findByName(String name);
  2.  
  3.     int batchUpdate(Map<String,Object> param);
  4.  
  5.     int batchUpdateCaseWhen(Map<String,Object> param);


实现类

  1.     /**
  2.      * 用于更新时,获取更新数据
  3.      * @param name
  4.      * @return
  5.      */
  6.     public List<Customer> findByName(String name) {
  7.         SqlSession sqlSession = null;
  8.         try {
  9.             sqlSession = SqlsessionUtil.getSqlSession();
  10.             return sqlSession.selectList("customer.findByName", name);
  11.         } catch (Exception e) {
  12.             e.printStackTrace();
  13.         } finally {
  14.             SqlsessionUtil.closeSession(sqlSession);
  15.         }
  16.         return new ArrayList<Customer>();
  17.     }
  18.  
  19.  
  20.     /**
  21.      * 批量更新第一种方式
  22.      * @param param
  23.      * @return
  24.      */
  25.     public int batchUpdate(Map<String,Object> param) {
  26.         return bathUpdate("customer.batchUpdate",param);
  27.     }
  28.  
  29.     /**
  30.      * 批量更新第二种方式
  31.      * @param param
  32.      * @return
  33.      */
  34.     public int batchUpdateCaseWhen(Map<String,Object> param) {
  35.         return bathUpdate("customer.batchUpdateCaseWhen",param);
  36.     }
  37.  
  38.     /**
  39.      * 公共部分提出
  40.      * @param statementId
  41.      * @param param
  42.      * @return
  43.      */
  44.     private int bathUpdate(String statementId,Map param){
  45.         SqlSession sqlSession = null;
  46.         try {
  47.             sqlSession = SqlsessionUtil.getSqlSession();
  48.             int key =  sqlSession.update(statementId, param);
  49.             // commit
  50.             sqlSession.commit();
  51.             return key;
  52.         } catch (Exception e) {
  53.             sqlSession.rollback();
  54.             e.printStackTrace();
  55.         } finally {
  56.             SqlsessionUtil.closeSession(sqlSession);
  57.         }
  58.         return 0;
  59.     }

测试前准备   首先用上节的 mybatis学习之路----批量更新数据 批量插入,插入10000条数据以备下面的批量更新用。

  1.     @Test
  2.     public void batchInsert() throws Exception {
  3.         Map<String,Object> param = new HashMap<String,Object>();
  4.         List<Customer> list = new ArrayList<Customer>();
  5.         for(int i=0;i<10000;i++){
  6.             Customer customer = new Customer();
  7.             customer.setName("准备数据" + i);
  8.             customer.setAge(15);
  9.             customer.setCeroNo("111111111111"+i);
  10.             customer.setCeroType(2);
  11.             customer.setSex(1);
  12.             list.add(customer);
  13.         }
  14.         param.put("list",list);
  15.         Long start = System.currentTimeMillis();
  16.         int result = customerDao.batchInsert(param);
  17.         System.out.println("耗时 "+(System.currentTimeMillis() - start));
  18.     }


开始进行测试效率问题。

首先进行的是测试十条数据。调整查询数据为查询十条

  1.     <!-- 这次用resultmap接收输出结果  -->
  2.     <select id="findByName" parameterType="string" resultMap="customerMap">
  3.         select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,10
  4.     </select>

测试类

  1.     @Test
  2.     public void batchudpate() throws Exception {
  3.         Map<String,Object> param = new HashMap<String,Object>();
  4.  
  5.         param.put("list",getFindByName("准备数据","批量更新01"));
  6.         Long start = System.currentTimeMillis();
  7.         customerDao.batchUpdate(param);
  8.         System.out.println("耗时 "+(System.currentTimeMillis() - start));
  9.     }
  10.  
  11.     @Test
  12.     public void batchudpateCaseWhen() throws Exception {
  13.         Map<String,Object> param = new HashMap<String,Object>();
  14.         param.put("list",getFindByName("批量更新01","准备数据"));
  15.         Long start = System.currentTimeMillis();
  16.         customerDao.batchUpdateCaseWhen(param);
  17.         System.out.println("耗时 "+(System.currentTimeMillis() - start));
  18.     }
  19.  
  20.     private List<Customer> getFindByName(String name, String change){
  21.         List<Customer> list = customerDao.findByName(name);
  22.         System.out.println("查询出来的条数 " + list.size());
  23.         if(null != change && !"".equals(change)){
  24.             for(Customer customer : list){
  25.                 customer.setName(change);
  26.             }
  27.         }
  28.  
  29.         return list;
  30.     }

第一种拼完整sql的方式耗时:

 

第二种case when 耗时情况:



结果可以看出,其实case when 耗时比较多。

 

下面来加大数据量到100条;

第一种拼完整sql的方式耗时:

 

第二种case when 耗时情况:


结果可以看出,其实case when 耗时仍然比第一种多。

 

继续加大数据量到1000

第一种拼完整sql的方式耗时:

 

第二种case when 耗时情况:

 

结果可以看出,其实case when 耗时仍然比第一种多。

 

继续加大数据量到10000

第一种拼完整sql的方式耗时:

 

第二种case when 耗时情况:

 

结果可以看出,两种方式进行批量更新,效率已经不在一个数量级了。case when明显的慢的多。

 

看网上有人说第一种的效率跟用代码循环着一条一条的循环着插入的效率差不多,通过测试我就有疑问了,他是怎么做到的。难道我的代码有问题?明明第一种的效率很高嘛。

 

                      第一种效率其实相当高的,因为它仅仅有一个循环体,只不过最后update语句比较多,量大了就有可能造成sql阻塞。

      第二种虽然最后只会有一条更新语句,但是xml中的循环体有点多,每一个case when 都要循环一遍list集合,所以大批量拼sql的时候会比较慢,所以效率问题严重。使用的时候建议分批插入。

根据效率,安全方面综合考虑,选择适合的很重要。

 

猜你喜欢

转载自blog.csdn.net/qq_29726869/article/details/81192699