int[] org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException
Rules:
1. Issue multiple update statements on a single PreparedStatement, using batch updates and a BatchPreparedStatementSetter to set values.
2. Will fall back to separate updates on a single PreparedStatement if the JDBC driver does not support batch updates.
Specified by:
Parameters:
Returns:
Throws:
Here is a reference code, where jdbcTemplate is already @Autowired.
public int[] insertPersons(List persons) {
return jdbcTemplate.batchUpdate(
"insert into staging.storage_insight_call_detail(first_name, middle_name, last_name) values(?, ?, ?)",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, persons.get(i).firstName());
ps.setString(2, persons.get(i).middleName());
ps.setString(3, persons.get(i).lastName());
}
public int getBatchSize() {
return persons.size();
}
});
}
0 comments :
Post a Comment