下面的方法是可行的,但是如何收集多个MapSqlParameterSource并将它们全部插入到一个批处理中呢?
new SimpleJdbcInsert(ds).withTableName(TABLENAME);
MapSqlParameterSource entry = new MapSqlParameterSource()
.addValue("id", report.queryId, Types.INTEGER)
.addValue("firstname", report.reportDate, Types.DATE)
.addValue("age", report.completionRatio, Types.INTEGER);
insert.execute(entry);发布于 2018-03-13 17:19:32
幸运的是,SimpleJdbcInsert可以接受MapSqlParameterSource的数组(而不是列表)。因此,可能如下所示:
List<MapSqlParameterSource> entries = new ArrayList<>();
entries.add(entry);
MapSqlParameterSource[] array = entries.toArray(new MapSqlParameterSource[entries.size()]);
insert.executeBatch(array);发布于 2020-03-10 21:52:24
有一种更好的方法可以用SqlParameterSourceUtils来实现
private final List<Map<String, Object>> records = new LinkedList<>();
final SimpleJdbcInsert statement = new SimpleJdbcInsert(dataSource)
.withTableName("stats")
.usingGeneratedKeyColumns("id")
.usingColumns("document", "error", "run", "celex");
statement.executeBatch(SqlParameterSourceUtils.createBatch(records));https://stackoverflow.com/questions/49239139
复制相似问题