返回参考游标和标量值的存储过程

Stored procedure returning Ref cursor and a scalar value

我在我们的应用程序中有一个要求,其中存储过程返回 1 个引用游标和 1 个标量参数,即状态代码。

现在我正在使用 Spring API 即 StoredProcedure 和 RowMapper class.

我能够执行存储过程,但是在执行方法调用后 Spring 没有调用我的 RowMapper 方法 MapRow。

下面是我的代码

DriverManagerDataSource ds = getDataSource();

        Map<String, Integer> inputValues = new HashMap<String, Integer>();
        inputValues.put("P_CLIENT_ID", java.sql.Types.VARCHAR);
        inputValues.put("P_REQ_TYP", java.sql.Types.VARCHAR);

        Map<String, RowMapper> outputMappers = new HashMap<String, RowMapper>();
        outputMappers.put("p_recordset", new SessionMgmtMapper());

        Map<String, Integer> outputValues = new HashMap<String, Integer>();
        outputValues.put("P_STATUS_CD", java.sql.Types.VARCHAR);

        Map<String, Object> valueMap = new HashMap<String, Object>();
        valueMap.put("P_CLIENT_ID", "0c1cab610a4445929932c09efe10225a");
        valueMap.put("P_REQ_TYP", "Authorization");

MultiMapperIOStoredProc multiMapperIOStoredProc = new MultiMapperIOStoredProc(ds, "GET_CLIENT_RS1", inputValues, outputValues, outputMappers);

multiMapperIOStoredProc.executeStoredProc(valueMap);

和我的 MultiMapperIOStoredProc 构造函数。

public MultiMapperIOStoredProc(final DataSource dataSource, final String storedProc,
            final Map<String, Integer> inputValues, final Map<String, Integer> outputValues,
            final Map<String, RowMapper> outputMappers) {
        super(dataSource, storedProc);

        if (null != inputValues && inputValues.size() > 0) {
            for (final String key : inputValues.keySet()) {
                this.declareParameter(new SqlParameter(key, inputValues.get(key)));
            }
        }
     // Pass multiple Mappers
        if (null != outputMappers && outputMappers.size() > 0) {
            for (final String key : outputMappers.keySet()) {
                this.declareParameter(new SqlOutParameter(key, OracleTypes.CURSOR, outputMappers.get(key)));
            }
        }

        if (null != outputValues && outputValues.size() > 0) {
            for (final String key : outputValues.keySet()) {
                this.declareParameter(new SqlOutParameter(key, outputValues.get(key)));
            }
        }

        this.compile();
    }

我的 executeStoredProc 方法

  public <T> List<T> executeStoredProc(final Map<String, Object> valueMap) {

        LOG.debug("executing stored procedure " + this.getSql() + " with values: " + valueMap);

        // execute stored procedure
        final Map<String, Object> resultMap = this.execute(valueMap);
        return null;
    }

知道如何进行这项工作。

所以只要稍作改动就可以了。根据 Spring 文档,我们必须按照它们在 Oracle DB 的底层存储过程中定义的顺序传递 IN 和 OUT 参数,但是如果您查看上面的代码,我正在使用 Map 的 HashMap 实现来设置我的 IN 和 OUT 参数根据 Java Doc.

不提供任何订购保证

我将所有 Map 实现从 HashMap 更改为 LinkedHashMap,如下所示。

// For IN params.
LinkedHashMap<String, Integer> inputValues = new LinkedHashMap<String, Integer>();

// for output values
Map<String, RowMapper> outputMappers = new LinkedHashMap<String, RowMapper>();

// For OUT params.
LinkedHashMap<String, Integer> outputValues = new LinkedHashMap<String, Integer>();

Spring 文档还显示了 Map 的用法,这是不正确的,需要更改为任何有序的数据结构。

这解决了我的问题。编码愉快。