【问题标题】:Stored Procedure returning multiple tables to spring jdbc template存储过程将多个表返回到spring jdbc模板
【发布时间】:2011-05-18 05:47:04
【问题描述】:

我正在使用 JdbcTemplate 从我的 Spring DAO 类中调用一个存储过程。我的问题是,存储过程返回多个表。有没有办法使用 Spring JdbcTemplate 访问多个表。

如果我使用 jdbcTemplate.queryForList(myStoredProc, new Object[]{parameters} 我只从结果中得到第一个表。

我的数据库是 SQL Server 2005。

除了 jdbcTemplate 之外还有什么方法可以满足我的要求吗?

【问题讨论】:

    标签: java stored-procedures spring-mvc spring-jdbc


    【解决方案1】:

    sinha 引用的解决方案对我不起作用。我能够使用JdbcTemplate#call(CallableStatementCreator, List<SqlParameter>) 解决这个问题。例如:

    private static final String sql = "{call schema_name.the_stored_procedure(?, ?, ?)}";
    
    // The input parameters of the stored procedure
    private static final List<SqlParameter> declaredParams = Arrays.asList(
        new SqlParameter("nameOfFirstInputParam", Types.VARCHAR),
        new SqlParameter("nameOfSecondInputParam", Types.VARCHAR),
        new SqlParameter("nameOfThirdInputParam", Types.VARCHAR));
    
    private static final CallableStatementCreatorFactory cscFactory
        = new CallableStatementCreatorFactory(sql, declaredParams);
    
    // The result sets of the stored procedure
    private static final List<SqlParameter> returnedParams = Arrays.<SqlParameter>asList(
        new SqlReturnResultSet("nameOfFirstResultSet", SomeRowMapper.INSTANCE),
        new SqlReturnResultSet("nameOfSecondResultSet", SomeOtherRowMapper.INSTANCE));
    
    public static Map<String, Object> call(JdbcTemplate jdbcTemplate,
                                           String param0,
                                           String param1,
                                           String param2) {
      final Map<String, Object> actualParams = new HashMap<>();
      actualParams.put("nameOfFirstInputParam", param0);
      actualParams.put("nameOfSecondInputParam", param1);
      actualParams.put("nameOfThirdInputParam", param2);
    
      CallableStatementCreator csc = cscFactory.newCallableStatementCreator(actualParams);
      Map<String, Object> results = jdbcTemplate.call(csc, returnedParams);
    
      // The returned map will including a mapping for each result set.
      //
      // {
      //   "nameOfFirstResultSet" -> List<SomeObject>
      //   "nameOfSecondResultSet" -> List<SomeOtherObject>
      // }
      //
      // For this example, we just return the heterogeneous map.  In practice,
      // it's better to return an object with more type information.  In other
      // words, don't make client code cast the result set lists.  Encapsulate
      // that casting within this method.
    
      return results;
    }
    

    【讨论】:

    • 谢谢! SqlReturnResultSet 是我要找的。 :)
    • 在单个查询中使用多个选择。这是我需要的查询。谢谢
    【解决方案2】:

    http://static.springsource.org/spring/docs/2.0.7/reference/jdbc.html#jdbc-StoredProcedure

    本节中给出的示例完全适用于存储过程返回多个结果集的情况。虽然这里给出的示例是针对 Oracle 的,但它也应该以同样的方式用于 MS SQL Server。

    【讨论】:

    • 我正在使用带有 proc 的 spring StoredProc 来返回标量或仅返回一行结果,但我很好奇如何处理这种情况。如果 proc 返回多行,您知道返回的 Map 的内容是什么吗?它是地图的地图,其中封闭的地图键是行索引?
    猜你喜欢
    • 2020-01-21
    • 2012-03-10
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2019-05-31
    • 2013-07-24
    相关资源
    最近更新 更多