Spring - Batch update using simplejdbccall while using Stored Procedure

会有一股神秘感。 提交于 2019-12-12 10:26:15

问题


I am using spring jdbc template, to create record using stored procedure

public Long create(City $obj) {
    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(getJdbcTemplate().getDataSource()).withProcedureName(SP_ADD_UPDATE);
    jdbcCall.declareParameters(new SqlOutParameter(ConstantUtil.RETUR_VAL, OracleTypes.BIGINT));
    Map<String, Object> jdbcCallResult = jdbcCall.execute(new MapSqlParameterSource(populateParams($obj)));
    return (Long) jdbcCallResult.get(ConstantUtil.RETUR_VAL);
}

params

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;

}

How can we batch update using simpleJdbcCall while using stored procedure ?


回答1:


I found a solution using 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;
    }    


来源:https://stackoverflow.com/questions/46865291/spring-batch-update-using-simplejdbccall-while-using-stored-procedure

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