我需要一次在数据库中插入数千条记录。我在我的应用程序中使用了spring JDBC模板。
下面是我到目前为止写的代码,它一次性执行所有的插入。所以,如果我有10,000个用户,他们是一次性插入的。但我想要的是成批执行它们,比如在一批中执行500条记录,等等。
@Override
public void saveBatch(final List<Employee> employeeList) {
final int batchSize = 500;
getJdbcTemplate().batchUpdate(QUERY_SAVE,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Employee employee = employeeList.get(i);
ps.setString(1, employee.getFirstname());
ps.setString(2, employee.getLastname());
ps.setString(3, employee.getEmployeeIdOnSourceSystem());
}
@Override
public int getBatchSize() {
return employeeList.size();
}
});
}
我如何修改上面的代码,以代替employeeList.size()作为批处理大小,我们可以将批处理大小设置为500,然后执行它们,然后执行next 500,依此类推?
请帮帮忙。
发布于 2012-03-05 20:31:14
我不确定您是否可以仅使用JDBC模板来做到这一点。也许您可以通过将大列表分割成批处理大小的块来分步调用batchUpdate
方法。
看看这里:
@Override
public void saveBatch(final List<Employee> employeeList) {
final int batchSize = 500;
for (int j = 0; j < employeeList.size(); j += batchSize) {
final List<Employee> batchList = employeeList.subList(j, j + batchSize > employeeList.size() ? employeeList.size() : j + batchSize);
getJdbcTemplate().batchUpdate(QUERY_SAVE,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Employee employee = batchList.get(i);
ps.setString(1, employee.getFirstname());
ps.setString(2, employee.getLastname());
ps.setString(3, employee.getEmployeeIdOnSourceSystem());
}
@Override
public int getBatchSize() {
return batchList.size();
}
});
}
}
发布于 2013-01-23 18:54:05
我知道现在有点晚了,但是你可以做一些类似@adarshr正在做的事情,除了使用Google Guava Lists.partition
来获取子列表。
public void saveBatch(final List<Employee> employeeList) {
final int batchSize = 500;
List<List<Employee>> batchLists = Lists.partition(employeeList, batchSize);
for(List<Employee> batch : batchLists) {
getJdbcTemplate().batchUpdate(QUERY_SAVE, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
Employee employee = batch.get(i);
ps.setString(1, employee.getFirstname());
ps.setString(2, employee.getLastname());
ps.setString(3, employee.getEmployeeIdOnSourceSystem());
}
@Override
public int getBatchSize() {
return batch.size();
}
});
}
}
发布于 2015-10-13 15:48:38
Spring提供具有多个批处理的批处理操作。在下面的示例中,批处理大小为100。
public class JdbcActorDao implements ActorDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public int[][] batchUpdate(final Collection<Actor> actors) {
int[][] updateCounts = jdbcTemplate.batchUpdate(
"update t_actor set first_name = ?, last_name = ? where id = ?",
actors,
100,
new ParameterizedPreparedStatementSetter<Actor>() {
public void setValues(PreparedStatement ps, Actor argument) throws SQLException {
ps.setString(1, argument.getFirstName());
ps.setString(2, argument.getLastName());
ps.setLong(3, argument.getId().longValue());
}
});
return updateCounts;
}
// ... additional methods
}
https://stackoverflow.com/questions/9565481
复制相似问题