【问题标题】:Calling Oracle procedure that returns rows using SimpleJdbcCall in Spring在 Spring 中调用使用 SimpleJdbcCall 返回行的 Oracle 过程
【发布时间】:2014-05-10 02:20:44
【问题描述】:

我写了以下代码

       MapSqlParameterSource in = new MapSqlParameterSource();
       in.addValue("V_OPP_ID", bean.getOpportunityId());
       in.addValue("V_NAME",bean.getName());
       in.addValue("V_FROM_DATE", bean.getStdate());
       in.addValue("V_TO_DATE", bean.getEddate());
       in.addValue("V_USERTYPE", bean.getUserType());
       jdbcCall.execute(in);

这里 jdbcCall.execute(in) 返回我对应于 Arraylist 的结果集/表。我如何提取这个 ArrayList

使用 jdbcCall 是正确的方法吗?如果不是,建议是什么?

【问题讨论】:

    标签: java spring spring-mvc oracle11g


    【解决方案1】:

    这是我用来调用函数的代码:

    RowMapper<String> rm = new ParameterizedRowMapper<String>() {
        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString(1);
        }
    };
    myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
            .withCatalogName("PACKAGE")
            .withFunctionName("GET_ALIAS")
            .returningResultSet("return", rm);
    
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("P_ID",userStr);
    params.addValue("P_DOMAIN_ALIAS", domain[0]);
    List<String> list = myStoredProcedure.executeFunction(List.class,params);
    

    如果您无法使用元数据,则代码如下:

    RowMapper<String> rm = new ParameterizedRowMapper<String>() {
        @Override
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString(1);
        }
    };
    SqlParameter emailParam = new SqlParameter("P_ID", OracleTypes.VARCHAR);
    SqlParameter domainParam = new SqlParameter("P_DOMAIN_ALIAS", OracleTypes.VARCHAR);
    SqlOutParameter resultParam = new SqlOutParameter("return", OracleTypes.CURSOR);
    myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
            .withCatalogName("PACKAGE")
            .withFunctionName("GET_ALIAS")
            .withoutProcedureColumnMetaDataAccess()
            .returningResultSet("return", rm)
            .declareParameters(resultParam, emailParam, domainParam);
    
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("P_ID",userStr);
    params.addValue("P_DOMAIN_ALIAS", domain[0]);
    List<String> list = myStoredProcedure.executeFunction(List.class,params);
    

    【讨论】:

    • 你喜欢解析字符串吗?为什么不使用 JdbcTemplate?
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2017-09-15
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多