normx222 :
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.
insert method
public void insertZygateData(List<ZygateEntity> parseData) {
String sql = "INSERT INTO Landing.midrange_xygate_load (account_name,command_name,system_name,CREATE_DT) VALUES (:account_name,:command_name,:system_name,:CREATE_DT);";
final int batchSize = 5;
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());
namedParameterJdbcTemplate.update(sql, source);
}
}
Andreas :
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()]));
}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=386099&siteId=1