Spring MVC 存储过程返回多个表到spring jdbc模板

q8l4jmvw  于 2023-10-24  发布在  Spring
关注(0)|答案(3)|浏览(99)

我正在使用JdbcTemplate从我的Spring DAO类调用存储过程。我的问题是,存储过程返回多个表。有没有一种方法可以使用Spring JdbcTemplate访问多个表。
如果我使用jdbcTemplate.queryForList(myStoredProc, new Object[]{parameters},iam只从结果中获取第一个表。
数据库是SQL Server 2005。
除了jdbcTemplate,还有其他方法可以满足我的需求吗?

nkhmeac6

nkhmeac61#

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;
}
hs1rzwqc

hs1rzwqc2#

请访问http://static.springsource.org/spring/docs/2.0.7/reference/jdbc.html#jdbc-StoredProcedure
本节中给出的示例正好适用于存储过程返回多个结果集的情况。虽然这里给出的示例适用于Oracle,但它也应该以同样的方式适用于MS SQL Server。

bkkx9g8r

bkkx9g8r3#

我们可以多次使用SimpleJdbcCallreturningResultSet来命名每个返回的结果集。下面是一个例子:下面给出返回两个表的SP的主体。

CREATE PROCEDURE [dbo].[USP_TestMultipleTables]
AS
BEGIN
    -- First Select Statement
    SELECT pid, [name]
    FROM Persons;

    -- Second Select Statement
    SELECT asset_id, description
    FROM Assets;
END;

我们可以使用SimpleJdbcCall来获得两个结果集,如下所示:

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate) // also valid: new SimpleJdbcCall(dataSource)
            .withProcedureName("USP_TestMultipleTables")
            .returningResultSet("persons", (rs, rowNum) -> {
                Person person = new Person();
                person.setPid(rs.getInt("pid"));
                person.setName(rs.getString("name"));
                return person;
            })
            .returningResultSet("assets", (rs, rowNum) -> {
                Asset asset = new Asset();
                asset.setId(rs.getInt("asset_id"));
                asset.setDescription(rs.getString("description"));
                return asset;
            });

    // Execute the stored procedure
    Map<String, Object> result = jdbcCall.execute();
    // 1.  Access persons ResultSet
    List<Person> persons = (List<Person>) result.get("persons");
    // 2. Access assets ResultSet
    List<Asset> assets = (List<Asset>) result.get("assets");

相关问题