Spring - 使用存储过程时使用simplejdbccall进行批量更新

时间:2017-10-21 16:16:18

标签: java stored-procedures spring-batch jdbctemplate spring-4

我使用spring jdbc模板,使用存储过程创建记录

private static Map<String, Object> populateParams(City $obj) {

    Map<String, Object> _params = new HashMap<String, Object>();
    _params.put("CITYID", $obj.getCityId());
    _params.put("CITYNAME", $obj.getCityName());
    _params.put("USERID", $obj.getCurUser());
    return _params;

}

PARAMS

{{1}}

如何在使用存储过程时使用simpleJdbcCall批量更新?

1 个答案:

答案 0 :(得分:1)

我找到了一个使用jdbcTemplate的解决方案:

public int insertBatch(final List<ExperimentUser> usersByExperiment)
            throws Exception {

        int[] insertedRows = getJdbcTemplate().batchUpdate("call myschema.myProc(?,?)",
                new BatchPreparedStatementSetter() {

                    @Override
                    public void setValues(PreparedStatement ps, int i)
                            throws SQLException {
                        ExperimentUser experimentUser = usersByExperiment
                                .get(i);
                        ps.setInt(1, experimentUser.getExperimentId());
                        ps.setString(2, experimentUser.getUniqueCode());                        
                    }

                    @Override
                    public int getBatchSize() {
                        return usersByExperiment.size();
                    }
                });
        return insertedRows.length;
    }