问题:
MyBatis使用批量插入问题
解决方案:
//直接注入获取本地
@Autowired
SqlSessionFactory sqlSessionFactory;
//使用SQLSession ,MyBatis的BATCH进行批量插入
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
DemoMapper domoMapper = sqlSession.getMapper(DemoMapper.class);
//批量的数量
int BATCH = 500;
for (int index = 0; index < insertList.size(); index++) {
domoMapper.insert(insertList.get(index));
i++;
if (index != 0 && index % BATCH == 0) {
sqlSession.commit();
}
}
sqlSession.commit();
}