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

发布于 2024-11-08 07:09:27 字数 264 浏览 0 评论 0原文

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

如果我使用 jdbcTemplate.queryForList(myStoredProc, new Object[]{参数} 我只从结果中获取第一个表。

我的数据库是SQL Server 2005。

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

Iam calling a stored procedure from my Spring DAO class using JdbcTemplate. My problem is that, stored procedure returns multiple tables. Is there a way to access multiple tables using Spring JdbcTemplate.

If I use
jdbcTemplate.queryForList(myStoredProc, new Object[]{parameters}
iam getting only first table from the result.

My database is SQL Server 2005.

Is there any method other than jdbcTemplate for my requirement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

季末如歌 2024-11-15 07:09:27

辛哈提到的解决方案对我不起作用。我能够使用 JdbcTemplate#call(CallableStatementCreator, List) 解决此问题。例如:

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

The solution sinha referenced didn't work for me. I was able to solve this using JdbcTemplate#call(CallableStatementCreator, List<SqlParameter>). For example:

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;
}
月牙弯弯 2024-11-15 07:09:27

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

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

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

The example given in this section is exactly for your case where the stored procedure returns multiple result-sets. Although the example given there is for Oracle, but it should work in the same way for MS SQL Server also.

蘑菇王子 2024-11-15 07:09:27

我们可以多次使用 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");

We can use SimpleJdbcCall's returningResultSet multiple time to name each returned resultset.
Below is an example:
Given below body of SP returning two tables.

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;

We can use SimpleJdbcCall to get the both result sets as below:

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文