SQLServer原因需要重写批量插入方法。 ps:注入SqlSessionFactory 时,如果是多数据源,需要@Qualifier指定数据源。 @Autowired private SqlSessionFactory sqlSessionFactory; public void save(String packageName, List list) { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false); try { //批量插入的数据分批处理 int batchCount = 200;//每批commit的个数 int batchIndex = (int) Math.ceil(list.size() / (double)batchCount);// 每批下标 List tempList = new ArrayList<>(batchCount); int start ,stop ; for (int i = 0; i < batchIndex ; i++) { tempList.clear(); start = i * batchCount; stop = Math.min(i * batchCount + batchCount - 1, list.size() - 1); logger.info("条数区间:" + start + " - " + stop); for (int j = start; j <= stop; j++) { tempList.add(list.get(j)); } sqlSession.insert(packageName, tempList); sqlSession.commit(); sqlSession.clearCache(); logger.info("已经插入" + (stop + 1) + " 条"); } } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); }finally { if (sqlSession != null) { sqlSession.close(); } }
mybatisPlus 批量插入数据SQLServer数据库
最新推荐文章于 2024-05-20 18:21:47 发布
本文讨论了在Spring框架中,当使用SqlSessionFactory处理多数据源时,如何重写批量插入方法以提高性能,包括数据分批处理、事务管理和异常处理。
摘要由CSDN通过智能技术生成