MyBatis批量新增和更新

阅读更多
网上参考了下总结:在连接SQL后面加上:&allowMultiQueries=true 才能实现批量更新

一、批量添加
1、XML
  
		      
		        SELECT  
		       	 	LAST_INSERT_ID() AS ID
		      
	    	INSERT INTO A (aa)   
	    	VALUES  
		      
		        (#{item.aa})
		      
	  

2、代码方法体

        int result = 0;
        if (collections == null || collections.isEmpty()) {
            return result;
        }
        SqlSession batchSqlSession = null;
        try {
            // 获取批量方式的sqlsession
            batchSqlSession = getSqlSessionTemplate().getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
            String insertSqlId = getStatement("addBatchs");
            // 每批commit的个数 > 100
            int startIndex = 0;
            int temps = 100;
            int size = collections.size();
            int count = (size % temps == 0) ? (size / temps) : (size / temps) + 1;
            boolean tag = false;
            for (int i = 0; i < count; i++) {
                if (tag) {
                    break;
                }
                if (temps > size) {
                    temps = size;
                    tag = true;
                }
                List tempLists = collections.subList(startIndex, temps);
                batchSqlSession.insert(insertSqlId, tempLists);
                result += tempLists.size();
                startIndex = temps;
                temps += 100;
                batchSqlSession.commit();
                batchSqlSession.clearCache();
            }
        } catch (Exception e) {
            batchSqlSession.rollback();
        } finally {
            batchSqlSession.close();
        }
        return result;
    


二 、更新
1、XML
  	
		    
		       	UPDATE 
		       		   
		       		
			        
					
				
		      
	  

2、方法体

        int result = 0;
        if (collections == null || collections.isEmpty()) {
            return result;
        }
        SqlSession batchSqlSession = null;
        try {
            // 获取批量方式的sqlsession
            batchSqlSession = getSqlSessionTemplate().getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
            String updateSqlId = getStatement("updateBatchs");
            // 每批commit的个数 > 100
            int startIndex = 0;
            int temps = IConstant.BATH_QUERY_SIZE;
            int size = collections.size();
            int count = (size % temps == 0) ? (size / temps) : (size / temps) + 1;
            boolean tag = false;
            for (int i = 0; i < count; i++) {
                if (tag) {
                    break;
                }
                if (temps > size) {
                    temps = size;
                    tag = true;
                }
                List tempLists = collections.subList(startIndex, temps);
               batchSqlSession.update(updateSqlId, tempLists);
                result += tempLists.size();
                startIndex = temps;
                temps += 100;
                batchSqlSession.commit();
                batchSqlSession.clearCache();
            }
        } catch (Exception e) {
            e.printStackTrace();            
            result = 0;
            batchSqlSession.rollback();
        } finally {
            batchSqlSession.close();
        }
        return result;
    

你可能感兴趣的:(mybatis,batch)