5

JdbcTemplate を使用して、Spring DAO クラスからストアド プロシージャを呼び出しています。私の問題は、ストアド プロシージャが複数のテーブルを返すことです。Spring JdbcTemplate を使用して複数のテーブルにアクセスする方法はありますか。

jdbcTemplate.queryForList(myStoredProc, new Object[]{parameters} iamを使用する と、結果から最初のテーブルのみが取得されます。

私のデータベースは SQL Server 2005 です。

私の要件に jdbcTemplate 以外の方法はありますか?

4

2 に答える 2

11

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;
}
于 2013-04-10T13:05:17.147 に答える
4

http://static.springsource.org/spring/docs/2.0.7/reference/jdbc.html#jdbc-StoredProcedureを参照してください。

このセクションで示す例は、ストアド プロシージャが複数の結果セットを返す場合に正確に当てはまります。そこに示されている例は Oracle 用ですが、MS SQL Server でも同じように機能するはずです。

于 2011-05-18T09:01:17.523 に答える