新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何在mybatis中批量更新数据

今天就跟大家聊聊有关如何在mybatis中批量更新数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

站在用户的角度思考问题,与客户深入沟通,找到民权网站设计与民权网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广、国际域名空间、虚拟主机、企业邮箱。业务覆盖民权地区。

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

 
  
    select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100
  
 
 
  
  
    
    
      update t_customer set
      c_name = #{cus.name},
      c_age = #{cus.age},
      c_sex = #{cus.sex},
      c_ceroNo = #{cus.ceroNo},
      c_ceroType = #{cus.ceroType}
      where id = #{cus.id}
    
  
 
  
  
    update t_customer
    
      
      
      
      
 
      
      
        
          
            when id=#{cus.id} then #{cus.name}
          
        
      
      
        
          
            when id=#{cus.id} then #{cus.age}
          
        
      
      
        
          
            when id=#{cus.id} then #{cus.sex}
          
        
      
      
        
          
            when id=#{cus.id} then #{cus.ceroNo}
          
        
      
      
        
          
            when id=#{cus.id} then #{cus.ceroType}
          
        
      
    
    
      
        id = #{cus.id}
      
    
  

接口

 List findByName(String name);
 
  int batchUpdate(Map param);
 
  int batchUpdateCaseWhen(Map param);

实现类

 /**
   * 用于更新时,获取更新数据
   * @param name
   * @return
   */
  public List findByName(String name) {
    SqlSession sqlSession = null;
    try {
      sqlSession = SqlsessionUtil.getSqlSession();
      return sqlSession.selectList("customer.findByName", name);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      SqlsessionUtil.closeSession(sqlSession);
    }
    return new ArrayList();
  }
 
 
  /**
   * 批量更新第一种方式
   * @param param
   * @return
   */
  public int batchUpdate(Map param) {
    return bathUpdate("customer.batchUpdate",param);
  }
 
  /**
   * 批量更新第二种方式
   * @param param
   * @return
   */
  public int batchUpdateCaseWhen(Map param) {
    return bathUpdate("customer.batchUpdateCaseWhen",param);
  }
 
  /**
   * 公共部分提出
   * @param statementId
   * @param param
   * @return
   */
  private int bathUpdate(String statementId,Map param){
    SqlSession sqlSession = null;
    try {
      sqlSession = SqlsessionUtil.getSqlSession();
      int key = sqlSession.update(statementId, param);
      // commit
      sqlSession.commit();
      return key;
    } catch (Exception e) {
      sqlSession.rollback();
      e.printStackTrace();
    } finally {
      SqlsessionUtil.closeSession(sqlSession);
    }
    return 0;
  }

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

@Test
  public void batchInsert() throws Exception {
    Map param = new HashMap();
    List list = new ArrayList();
    for(int i=0;i<10000;i++){
      Customer customer = new Customer();
      customer.setName("准备数据" + i);
      customer.setAge(15);
      customer.setCeroNo("111111111111"+i);
      customer.setCeroType(2);
      customer.setSex(1);
      list.add(customer);
    }
    param.put("list",list);
    Long start = System.currentTimeMillis();
    int result = customerDao.batchInsert(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }

开始进行测试效率问题。

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

  
  
    select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,10
  

测试类

  @Test
  public void batchudpate() throws Exception {
    Map param = new HashMap();
 
    param.put("list",getFindByName("准备数据","批量更新01"));
    Long start = System.currentTimeMillis();
    customerDao.batchUpdate(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }
 
  @Test
  public void batchudpateCaseWhen() throws Exception {
    Map param = new HashMap();
    param.put("list",getFindByName("批量更新01","准备数据"));
    Long start = System.currentTimeMillis();
    customerDao.batchUpdateCaseWhen(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }
 
  private List getFindByName(String name, String change){
    List list = customerDao.findByName(name);
    System.out.println("查询出来的条数 : " + list.size());
    if(null != change && !"".equals(change)){
      for(Customer customer : list){
        customer.setName(change);
      }
    }
 
    return list;
  }

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

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

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

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

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

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

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

继续加大数据量到1000条

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

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

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

继续加大数据量到10000条

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

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

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

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

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

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

看完上述内容,你们对如何在mybatis中批量更新数据有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


标题名称:如何在mybatis中批量更新数据
当前URL:http://www.wjwzjz.com/article/ghceeg.html
在线咨询
服务热线
服务热线:028-86922220
TOP