Java 这个spring类BatchPreparedStatementSetter有什么用途?
有人能给我简单描述一下他的春季班吗Java 这个spring类BatchPreparedStatementSetter有什么用途?,java,spring,spring-jdbc,Java,Spring,Spring Jdbc,有人能给我简单描述一下他的春季班吗 org.springframework.jdbc.core.BatchPreparedStatementSetter ()它用于一次批量插入多行 将说明如何使用它 仔细看看importTemployes方法,一切都应该变得清晰起来。可以使用JdbcTemplate batchUpdate方法进行批更新,如下所示。 public int[] batchUpdate(final List<Actor> actors) { int[] updateCo
org.springframework.jdbc.core.BatchPreparedStatementSetter
()它用于一次批量插入多行 将说明如何使用它
仔细看看importTemployes方法,一切都应该变得清晰起来。可以使用JdbcTemplate batchUpdate方法进行批更新,如下所示。
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;
}
public int[]批更新(最终参与者列表){
int[]updateCounts=jdbcTemplate.batchUpdate(“更新t_actor set first_name=?,”+
“姓氏=?其中id=?”,
新的BatchPreparedStatementSetter(){
公共void setValues(PreparedStatement ps,int i)引发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(){
返回actors.size();
}
});
返回更新计数;
}
Hi Ashok,我有点困惑,比如如果我必须实现一个类的BatchPreparedStatementSetter接口,那么我如何才能创建ps对象。我知道我们可以为该类创建一个用于对象传递的参数化构造函数,但我将从中获取ps对象。可能是由于尝试执行ps.setString时该类中出现空指针异常。