1、数据库一定安装在linux系统上面,这样比较稳定,如果配置读写分离也可以。
2、数据库连接url后面追加&rewriteBatchedStatements=true,至于原因请大家去看jdbc驱动里面PreparedStatement源码
3、上代码:
Connection connection= null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
ResultSet rs=null;
try {
//从当前sqlsession获取连接,相当于把后面的批量插入的事务交给当前事务管理。如果是纯jdbc需要自己手动去管理
connection= sqlSessionTemplate.getConnection();
ps = connection.prepareStatement(
"insert into xxx(id,name) values(?,?)",PreparedStatement.RETURN_GENERATED_KEYS);//返回自增主键配置
for (int i = 0; i < details.size(); i++) {
SampleDetail sampleDetail= details.get(i);
ps.setInt(1,sampleDetail.getId());
ps.setString(2,sampleDetail.getName());
ps.addBatch();//添加到批次
}
ps.executeBatch();//提交批处理