mybatis 批量更新(batchUpdate)

第一:修改数据库的连接方式 &&allowMultiQueries=true

比如:jdbc\:mysql\://rdshme0env6yf2n829ympublic.mysql.rds.aliyuncs.com/wlsq_dev_test?useUnicode\=true&characterEncoding\=utf8&allowMultiQueries=true


mybatis 配置文件:

   
       
          
				update account set `password` = #{item.password} where acct_id = #{item.acctId}				
	       
     


dao 层

  public void updateBatch(List list);

service 层

  public void updateBatch(List list);

service 实现层

	public void updateBatch(List list) {
		// TODO Auto-generated method stub
		this.accountMapper.updateBatch(list);
	}

controller 层

 @RequestMapping({"/batchupdate"})
  @ResponseBody
  public String batchUpdate(){
	  String result = "";
	  try{
	  List list = accountService.batchUpdate();
	  List batch = new ArrayList();
	  
	 
	  //密码修改
	  if(list !=null && list.size()>0){
		  for(int i=0;i a = new ArrayList();
           if (i == batchnum - 1) {  
               int size = (i + 1) * init;  
               for (int j = size - init; j < nums; j++) {  
            	   if (batch.get(j) == null) {  
                       break;  
                   } 
            	   a.add(batch.get(j));  
               }  
              
           } else {  
               int size = (i + 1) * init;  
               for (int j = size - init; j < size; j++) { 
            	   if (batch.get(j) == null) {  
                       break;  
                   }  
            	   a.add(batch.get(j));  
               }  
             
           }  
           accountService.updateBatch(a);
          
       }  
	  
	 
	
	  
	  }catch(Exception e){
		  System.out.println(e.getMessage());
		  return "false";
	  }
	  result = "true";
	  return result;
	  
  }

结果展示:

mybatis 批量更新(batchUpdate)_第1张图片

你可能感兴趣的:(MyBaties(基础篇))