Reputation: 25
I am parsing a file and am creating a list of string elements that im inserting in to my table. Im trying to set a batch size of 5 rows inserted at a time and can't figure out how to use .batchupdate
in place of .update
in my code
Upvotes: 1
Views: 5116
Reputation: 159086
You're currently calling update(String sql, SqlParameterSource paramSource)
.
The comparable batch version is batchUpdate(String sql, SqlParameterSource[] batchArgs)
.
So it seems pretty obvious, to do it as a batch, build an array, and make the call.
final int batchSize = 5;
List<SqlParameterSource> args = new ArrayList<>();
for (ZygateEntity zygateInfo : parseData){
SqlParameterSource source = new MapSqlParameterSource("account_name", zygateInfo.getAccountName())
.addValue("command_name", zygateInfo.getCommandName())
.addValue("system_name", zygateInfo.getSystemName())
.addValue("CREATE_DT", zygateInfo.getCreateDt());
args.add(source);
if (args.size() == batchSize) {
namedParameterJdbcTemplate.batchUpdate(sql, args.toArray(new SqlParameterSource[args.size()]));
args.clear();
}
}
if (! args.isEmpty()) {
namedParameterJdbcTemplate.batchUpdate(sql, args.toArray(new SqlParameterSource[args.size()]));
}
Upvotes: 6