selectSql="select * from tb_monitor_data_15m"; List<Map<String, Object>> sqlList = jdbcTemplate.queryForList(selectSql); 循环遍历 实现分批每2000次插入数据
时间: 2024-06-06 08:10:51 浏览: 172
在Spring中用select last_insert_id()时遇到问题
String insertSql = "insert into tb_monitor_data_15m (col1, col2, col3) values (?, ?, ?)";
int batchSize = 2000;
int count = 0;
List<Object[]> batchValues = new ArrayList<>();
for (Map<String, Object> sqlMap : sqlList) {
Object[] values = new Object[]{sqlMap.get("col1"), sqlMap.get("col2"), sqlMap.get("col3")};
batchValues.add(values);
count++;
if (count % batchSize == 0) {
jdbcTemplate.batchUpdate(insertSql, batchValues);
batchValues.clear();
}
}
if (!batchValues.isEmpty()) {
jdbcTemplate.batchUpdate(insertSql, batchValues);
}
阅读全文