mybatis 批量插入list对象集合

针对数据库使用oracle数据库:

第一种:开始设计id用的是数据库的 SEQUENCE 

mybatis如下:



    insert into wms_inbound (ID,INBOUND_CODE)
    SELECT 
        SEQ_ORDER_DETAILS.NEXTVAL,A.* FROM(
  
    select #{item.orderCode,jdbcType=VARCHAR}, #{item.orderHawbcode,jdbcType=VARCHAR} from dual 
 ) A


执行的sql我单独拿出来在plsql执行了下,耗时:

第二种:几乎和第一种一样



    insert into wms_inbound (ID,INBOUND_CODE) 
select * from(
  
      select
        #{item.id,jdbcType=VARCHAR}, #{item.inboundCode,jdbcType=VARCHAR}
      from dual
  )

这种方式和第一种几乎一样,耗时:


第三种:在java代码里,其中

wmsInboundMapper.insert调用的是mybatis自动生成的单条插入方法
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
public int saveBatch(List wmsInbounds){
		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);

		int size = 10000;
		int result=0;
		try{
			for(int i = 0; i < wmsInbounds.size(); i++) {
				wmsInboundMapper.insert(wmsInbounds.get(i));
				result++;
				if(i % 1000 == 0 || i == size - 1) {
					//手动每1000个一提交,提交后无法回滚
					session.commit();
					//清理缓存,防止溢出
					session.clearCache();
				}
			}
		} catch (Exception e) {
			//没有提交的数据可以回滚
			session.rollback();
		} finally{
			session.close();
		}
		return result;
	}

执行时间为,4.5秒,:

mybatis 批量插入list对象集合_第1张图片

传入集合为:list<对象>



你可能感兴趣的:(mybatis)