|
From: <tri...@tr...> - 2003-11-26 23:39:47
|
All,
I have finally had a block of time to devote to implementing the "Mapping"
feature for rows in the resultsets returned by a stored procedure. It is
similar to the mapping of results returned in a query except that a stored
procedure can return multiple resultsets. Each resultset's rows will be mapped
to an object that is placed in a List. This List is returned in the Map
returned by execute - the same way return values for other output parameters are
returned.
To use this feature you must create a class (probably an inner class defined in
your class that extends StoredProcedure) that implements the new interface
RowMapper. Here is an example:
private class PetMapper implements RowMapper {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Pet p = new Pet();
p.setId(rs.getInt(1));
p.setName(rs.getString(2));
return p;
}
}
When you specify the out parameter that returns the resultset you pass in this
RowMapper class as the third parameter. Here is an Oracle example:
declareParameter(new SqlOutParameter("rs", oracle.jdbc.OracleTypes.CURSOR, new
PetMapper()));
If you use SQL Server or Sybase and I think DB2 then you would use
SqlReturnResultSet with the RowMapper as the second parameter. Like this:
declareParameter(new SqlReturnResultSet("rs", new PetMapper()));
You would retrieve the List of rows from the Map returned by the execute method
of the StoredProcedure.
List rs = (List) resultMap.get("rs");
I have added a test to the StoredProcedureTestSuite and I have also added a test
for the recently reintroduced ParameterMapper interface. You can look at these
for an example on how to use this feature. If you want a complete example, let
me know and I will post one.
Thomas
|