站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 1.2.9 API 文档英文版

RowMapperResultReader (Spring Framework) - Spring Framework 1.2.9 API 文档英文版


org.springframework.jdbc.core
Class RowMapperResultReader

java.lang.Object
  extended by org.springframework.jdbc.core.RowMapperResultReader
All Implemented Interfaces:
ResultReader, RowCallbackHandler

public class RowMapperResultReader
extends Object
implements ResultReader

Adapter implementation of the ResultReader interface that delegates to a RowMapper which is supposed to create an object for each row. Each object is added to the results list of this ResultReader.

Useful for the typical case of one object per row in the database table. The number of entries in the results list will match the number of rows.

Note that a RowMapper object is typically stateless and thus reusable; just the RowMapperResultReader adapter is stateful.

A usage example with JdbcTemplate:

 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object
 RowMapper rowMapper = new UserRowMapper();  // reusable object

 List allUsers = jdbcTemplate.query(
     "select * from user",
     new RowMapperResultReader(rowMapper, 10));

 User user = (User) jdbcTemplate.queryForObject(
     "select * from user where id=?", new Object[] {id},
     new RowMapperResultReader(rowMapper, 1));

Alternatively, consider subclassing MappingSqlQuery from the jdbc.object package: Instead of working with separate JdbcTemplate and RowMapper objects, you can have executable query objects (containing row-mapping logic) there.

Since:
1.0.2
Author:
Juergen Hoeller
See Also:
RowMapper, DataAccessUtils.singleResult(java.util.Collection), MappingSqlQuery

Constructor Summary
RowMapperResultReader(RowMapper rowMapper)
          Create a new RowMapperResultReader.
RowMapperResultReader(RowMapper rowMapper, int rowsExpected)
          Create a new RowMapperResultReader.
 
Method Summary
 List getResults()
          Return all results, disconnected from the JDBC ResultSet.
 void processRow(ResultSet rs)
          Implementations must implement this method to process each row of data in the ResultSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RowMapperResultReader

public RowMapperResultReader(RowMapper rowMapper)
Create a new RowMapperResultReader.

Parameters:
rowMapper - the RowMapper which creates an object for each row

RowMapperResultReader

public RowMapperResultReader(RowMapper rowMapper,
                             int rowsExpected)
Create a new RowMapperResultReader.

Parameters:
rowMapper - the RowMapper which creates an object for each row
rowsExpected - the number of expected rows (just used for optimized collection handling)
Method Detail

processRow

public void processRow(ResultSet rs)
                throws SQLException
Description copied from interface: RowCallbackHandler
Implementations must implement this method to process each row of data in the ResultSet. This method should not call next() on the ResultSet; it is only supposed to extract values of the current row.

Exactly what the implementation chooses to do is up to it: A trivial implementation might simply count rows, while another implementation might build an XML document.

Specified by:
processRow in interface RowCallbackHandler
Parameters:
rs - the ResultSet to process (pre-initialized for the current row)
Throws:
SQLException - if a SQLException is encountered getting column values (that is, there's no need to catch SQLException)

getResults

public List getResults()
Description copied from interface: ResultReader
Return all results, disconnected from the JDBC ResultSet. Never returns null; returns the empty collection if there were no results.

Specified by:
getResults in interface ResultReader


Copyright (c) 2002-2007 The Spring Framework Project.