0

我的 Oracle DB 中有一个存储过程,它返回一个 CURSOR 并且工作正常。我想在没有任何对象持久性的情况下在 Java 中检索游标的数据。我唯一的目标是将其显示到屏幕上。由于我将有几十个存储过程,并且我不想为每个存储过程创建一个新的 RowMapper,所以我尝试使用 RowSetDynaClass。

我继承了 Spring StoredProcedure 对象,如下所示:

public abstract class AbstractReport extends StoredProcedure {

    public static final String KEY_REPORT_RESULT = "profiles";

    public AbstractReport(DataSource dataSource, String sprocName) {
        super(dataSource, sprocName);
        declareParameter(new SqlOutParameter(
                KEY_REPORT_RESULT,
                oracle.jdbc.OracleTypes.CURSOR,
                new RowMapper() {
                    public RowSetDynaClass mapRow(ResultSet argResults, int argRowNum ) throws SQLException {
                        return new RowSetDynaClass(argResults);
                    }
                })
        );
//      declareParameter(new SqlOutParameter(
//              "test",
//              oracle.jdbc.OracleTypes.CURSOR,
//              new RowMapper() {
//                  public String[] mapRow(ResultSet argResults, int argRowNum ) throws SQLException {
//                      return new String[]{argResults.getString(1), argResults.getString(2), argResults.getString(3)};
//                  }
//              })
//        );
        compile();
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> computeReport(Map<String, Object> params){
        return super.execute(params);
    }
...

}

这被称为:

public List<String[]> computeReport(Map<String, Object> params, ReportEnum report) throws EptServiceException {
        List<String[]> result;

        try {
            logger.debug("Computing report " + report);
            AbstractReport procedure = getReport(report);

            Map<String, Object> rawResult = procedure.computeReport(params);
            logger.info("rawResult:" + rawResult);
            result = generateReportOutput(rawResult);
        } catch (EptServiceException e) {
            throw e;
        } catch (Exception e) {
            logger.error("Error while computing report:" + report, e);
            throw new EptServiceException(EsbPortalError.ERROR_PORTAL_UNKNOWN, e);
        }

        return result;
    }

除了我从结果中遗漏了一行之外,所有这些工作几乎都非常好......当我在 SQL 中执行存储过程时,我有 3 个结果。如果我使用像上面评论中的那样简单的 RowMapper,我会得到 3 个结果。

But if I use RowSetDynaClass I get one object with 2 rows, instead of the 3 expected. I know it doesn't make much sense (at least it doesn't for me) but there must be something stupid I'm missing.

What am I doing wrong?

4

1 回答 1

1

I finally figured it out (mostly). I couldn't find a way to make it work using RowMapper interface. Thus, I used the ResultSetExtractor object as following:

declareParameter(new SqlOutParameter(
            KEY_REPORT_RESULT,
            oracle.jdbc.OracleTypes.CURSOR,
            new ResultSetExtractor() {

                @Override
                public Object extractData(ResultSet rs) throws SQLException,
                        DataAccessException {
                    return new RowSetDynaClass(rs);
                }
            })
);

This output me the correct three results I was expecting. As for why RowMapper does not work but this one does I don't know.

于 2013-04-10T14:22:13.453 回答