oracle批量入库解决方案
作者:互联网
思路:jdbcTemplate
核心代码两个:
1、batchInserPrepard入库
@Autowired
private JdbcTemplate jdbcTemplate;
public int batchInsertPrePared(List<MyOBject> list){
String sql="insert into T_MY_TABLE values(?,?,?,?)";
int[] insertCountArray = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, list.get(i).getId());//主键
ps.setString(1, list.get(i).getColumnsOne());
ps.setString(1, list.get(i).getColumnsTwo());
ps.setString(1, list.get(i).getColumnsThree());
}
@Override
public int getBatchSize() {
return list.size();
}
});
int sumInsertedCount = 0;
for (int a : insertCountArray) {
sumInsertedCount += a;
}
return insertCountArray.length;
}
}
2、解析组装list对象方法
//解析代码(只列核心代码)
List<TMOBJECT> objectList = new ArrayList<TMOBJECT>(2000);//性能优化防止自东扩. 容,直接设置默认值
objectList.add(temp);
if (objectList.size() == 2000) {
int a = tMYObjectService.batchInsertPrePared(objectList);
sumInsertedCount += a;
logger.info("批次:" + objectList.size() + "入库完毕");
objectList.clear();
}
}
实现原理:使用batchInsertPrePared批量入库,每两千一批次,设置list的默认值大小,防止list自东扩容提高性能。batchInsertPrePared 执行完毕后,clear() list对象。核心代码如上所示。
标签:ps,批量,get,int,setString,list,oracle,入库,objectList 来源: https://blog.csdn.net/u011153223/article/details/121152286