场景:大批量新增数据
对比:逐笔提交和批次提交
记录数:2322条记录
测试结果:单笔提交:22938毫秒;批量提交:172毫秒
批量提交相关代码:
connection = dataSource.getConnection();
connection.setAutoCommit(false);
// 组装新增sql
String sql = assembleInsertSql();
statement = connection
.prepareStatement(sql);
for (int i = 0; i < beans.size(); i++) {
// 组装statement参数
aseembleStatement(statement, beans.get(i));
statement.addBatch();
// 每1000笔提交一次
if (i % 1000 == 0) {
statement.executeBatch();
connection.commit();
statement.clearBatch();
}
}
statement.executeBatch();
connection.commit();