How to get multi table results of an stored procedure using SimpleJDBCCall in spring?

戏子无情 提交于 2021-01-24 13:19:09

问题


I'm implementing a Spring+ MSSQL Server 2008 application. I use SimpleJDBCCall API to execute stored procedures and retrieve results.

For stored procedures with mono table results, it works fine, but I don't know how to use it for procedures with multi table results.

Sample procedure body:

multi table results

    SELECT * FROM TABLE1
    SELECT * FROM TABLE2

回答1:


I was most ignorant, it does in fact work! You can specify both resultsets, with each its own mapper. In code it looks like this:

SimpleJdbcCall call = new SimpleJdbcCall(this.jdbc)
           .withProcedureName("get_users3")
           .returningResultSet("rs1", new ParameterizedRowMapper<Object[]>()
           {
              @Override
              public Object[] mapRow(ResultSet rs, int rowNum) throws SQLException
              {
                 return new Object[] { rowNum, rs.getInt(1), rs.getString(2) };
              }
           })
           .returningResultSet("rs2", new ParameterizedRowMapper<Object[]>()
           {
              @Override
              public Object[] mapRow(ResultSet rs, int rowNum) throws SQLException
              {
                 return new Object[] { rowNum, rs.getInt(1), rs.getString(2) };
              }
           });

  Map<String, Object> res = call.execute();
  assertNotNull(res.get("rs1"));
  assertNotNull(res.get("rs2"));
  List<Object[]> l1 = (List<Object[]>)res.get("rs1");
  List<Object[]> l2 = (List<Object[]>)res.get("rs2");


来源:https://stackoverflow.com/questions/15403504/how-to-get-multi-table-results-of-an-stored-procedure-using-simplejdbccall-in-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!