在spring中使用jdbcTemplate进行批量更新

时间:2013-10-04 12:32:53

标签: java spring hashmap jdbctemplate batch-updates

我已经看到了批量更新的代码,如下面的使用ArrayList:

@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();
                }
            });

    }
}

但如果我使用如下的Hashmap:HashMap<String, VerifyPaymentRO> verifyPaymentInfoMap

VerifyPaymentRO是java bean

如何使用它来更新记录?

1 个答案:

答案 0 :(得分:1)

只需从地图值中创建List<?>,您就可以使用相同的代码:

List<VerifyPaymentRO> verifyPaymentList = new ArrayList<>(verifyPaymentInfoMap.values());