插入
大规模插入数据时使用批量执行模式
MySQL
需在连接参数增加rewriteBatchedStatements=true
Oracle
默认支持但不能返回影响条数
spring
中代码示例:
@Autowired
SqlSessionFactory sqlSessionFactory;
@Test
public void insert() throws SQLException {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
TestTableMapper mapper = sqlSession.getMapper(TestTableMapper.class);
long time = System.currentTimeMillis();
for (int i = 0; i < 1_000_000; i++) {
mapper.insert(i);
if (i % 100_000 == 0) {
sqlSession.commit();
LOGGER.info("{}", i);
}
}
sqlSession.commit();
System.out.println(System.currentTimeMillis() - time);
}