sqlSessionFactory achieve java batch submitted, but can not return the number of affected.
1 public int updateBatch(List<Object> list){ 2 if(list ==null || list.size() <= 0){ 3 return -1; 4 } 5 SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean("sqlSessionFactory"); 6 SqlSession sqlSession = null; 7 try { 8 sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false); 9 Mapper mapper = sqlSession.getMapper(Mapper.class); 10 int batchCount = 1000; // number of submissions, the number of arriving submitted . 11 for ( int index = 0; index <list.size (); index ++ ) { 12 is Object obj = List.get (index); 13 is Mapper .updateInfo (obj); 14 IF (index = 0 == 0 && index batchCount%! ) { 15 sqlSession.commit (); 16 } . 17 } 18 is sqlSession.commit (); . 19 return 0 ; 20 is }catch (Exception e){ 21 sqlSession.rollback(); 22 return -2; 23 }finally { 24 if(sqlSession != null){ 25 sqlSession.close(); 26 } 27 } 28 29 }
@Component public class SpringContextUtil implements ApplicationContextAware{ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext(){ return applicationContext; } public static Object getBean(Class T){ try { return applicationContext.getBean(T); }catch (BeansException e){ return null; } } public static Object getBean(String name){ try { return applicationContext.getBean(name); }catch (BeansException e){ return null; } } }