插入大量数据:
1.jdbc
2.MyBatis的批量插入
3.调用存储过程
long start = System.currentTimeMillis();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/springbatch", "root", "123456");
connection.setAutoCommit(false);
String sql = "insert into person(name,age,nation,address) VALUES(?,?,?,?)";
PreparedStatement prepareStatement = connection.prepareStatement(sql);
int batchSize=0;
for (int i = 0; i < 1000000; i++) {
prepareStatement.setString(1, "cat");
prepareStatement.setString(2, "110");
prepareStatement.setString(3, "China");
prepareStatement.setString(4, "China");
prepareStatement.addBatch();
if(batchSize==10000){
prepareStatement.executeBatch();
connection.commit();
batchSize=0;
prepareStatement.clearBatch();
}else {
batchSize=batchSize+1;
}
}
if(batchSize>0){
prepareStatement.executeBatch();
connection.commit();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
long end = System.currentTimeMillis();
long result=end - start;
System.out.println("耗时:"+result + "");