- jdbc 鏈接需要加入
rewriteBatchedStatements=true
配置
spring:
# 配置數(shù)據(jù)源
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true
username: root
password: root
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
connection.setAutoCommit(false);
String sql = "insert into test(id,name) values(?,?)";
statement = connection.prepareStatement(sql);
for (int i = 1; i < 100000; i++) {
statement.setInt(1, i);
statement.setString(2, "" + i);
statement.addBatch();
if (i % 1000 == 0) {
statement.executeBatch();
connection.commit();
statement.clearBatch();
}
}
statement.executeBatch();
connection.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.release(connection, statement, resultSet);
}
}