Spring+SpringMVC+Mybatis操作大数据(上万条数据读写)

  • Mapper

	    insert into  ${tableName} (id,name,....)
	    values
	     
		   	(
		   	#{ss.id,jdbcType=BIGINT},
            #{ss.name,jdbcType=VARCHAR}
            .....
	      	)
	      

SERVICE

public class xxImpl implements xxService{
    @Autowired  
	private SqlSessionTemplate sqlSessionTemplate;  

    @Override
    public int xxInsert(List list){


        //做一些业务逻辑操作
        sqlSession=
        sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,true);
	    StakeptCustomMapper xxmapper = sqlSession.getMapper(xx.class);
		List subList=new ArrayList();
		int i=0;
		for (XX xx: list) {
			i++;
			subList.add(xx);
			if(i%100==0||i%(list.size())==0){
				xxmapper .insert(subList,tableName);
				subList.clear();
			}
		}
		sqlSession.commit();//提交事务
    }
}

springmvc配置



		

小结

1.数据库循环插入

2.业务逻辑层分批插入

3.如果服务器响应需要耗费大量时间处理业务逻辑  可以采用Spring自带的@Async  异步请求 先响应后处理

【转载】mybatis三种批量插入方式对比:https://blog.csdn.net/m0_37981235/article/details/79131493

 

你可能感兴趣的:(Spring+SpringMVC+Mybatis操作大数据(上万条数据读写))