gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:13 29 4
gpt4 key购买 nike

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

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

我的数据库是 SQL Server 2005。

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

最佳答案

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

关于java - 将多个表返回到 spring jdbc 模板的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6040385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com