What is the use of this spring class BatchPreparedStatementSetter?

牧云@^-^@ 提交于 2019-12-10 15:38:27

问题


Can anyone give me short description about his spring class

org.springframework.jdbc.core.BatchPreparedStatementSetter

(JavaDoc API Link)


回答1:


It's used for bulk insertion of many rows at once.

This code will illustrate how it's used.

Take a good look at importEmployees method, and everything should become clear.




回答2:


batchUpdate can be done using JdbcTemplate batchUpdate method as follows..

public int[] batchUpdate(final List<Actor> actors) {
int[] updateCounts = jdbcTemplate.batchUpdate("update t_actor set first_name = ?, " +
"last_name = ? where id = ?",
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, actors.get(i).getFirstName());
ps.setString(2, actors.get(i).getLastName());
ps.setLong(3, actors.get(i).getId().longValue());
}
public int getBatchSize() {
return actors.size();
}
});
return updateCounts;
}


来源:https://stackoverflow.com/questions/4321784/what-is-the-use-of-this-spring-class-batchpreparedstatementsetter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!