Such manual mapping is usually preferable to "automatic" mapping using reflection, which can become complex in non-trivial cases. For example, the present class allows different objects to be used for different rows (for example, if a subclass is indicated). It allows computed fields to be set. And there's no need for ResultSet columns to have the same names as bean properties. The Pareto Principle in action: going the extra mile to automate the extraction process makes the framework much more complex and delivers little real benefit.
Subclasses can be constructed providing SQL, parameter types and a DataSource. SQL will often vary between subclasses.
org.springframework.jdbc.object.MappingSqlQuery , org.springframework.jdbc.object.SqlQuery
| Modifier and Type | Class and Description |
|---|---|
| protected class | MappingSqlQueryWithParameters.RowMapperImpl
Implementation of RowMapper that calls the enclosing
class's |
| Access | Constructor and Description |
|---|---|
| public | |
| public | MappingSqlQueryWithParameters(DataSource
the DataSource to use to get connections ds, String the SQL to run sql)Convenient constructor with DataSource and SQL string. |
| Modifier and Type | Method and Description |
|---|---|
| protected abstract T | Returns: an object of the result typethe ResultSet we're working through rs, int row number (from 0) we're up to rowNum, Object[] to the query (passed to the execute() method).
Subclasses are rarely interested in these.
It can be parameters, Map<?, ?> null if there are no parameters.passed to the execute() method.
It can be context)null if no contextual information is need.Subclasses must implement this method to convert each row of the ResultSet into an object of the result type. |
| protected RowMapper | newRowMapper(Object[]
the parameters to the parameters, Map<?, ?> execute() method,
in case subclass is interested; may be null if there
were no parameters.the contextual information passed to the context)mapRow
callback method. The JDBC operation itself doesn't rely on this parameter,
but it can be useful for creating the objects of the result list.Implements abstract org. |
| MappingSqlQueryWithParameters | back to summary |
|---|---|
| public MappingSqlQueryWithParameters() Constructor to allow use as a JavaBean. | |
| MappingSqlQueryWithParameters | back to summary |
|---|---|
| public MappingSqlQueryWithParameters(DataSource ds, String sql) Convenient constructor with DataSource and SQL string.
| |
| mapRow | back to summary |
|---|---|
| protected abstract T mapRow(ResultSet rs, int rowNum, Object[] parameters, Map<?, ?> context) throws SQLException Subclasses must implement this method to convert each row of the ResultSet into an object of the result type.
| |
| newRowMapper | back to summary |
|---|---|
| protected RowMapper Implements abstract org. Implementation of protected abstract method. This invokes the subclass's implementation of the mapRow() method.
| |
mapRow method for each row.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public T | mapRow(ResultSet
the rs, int ResultSet to map (pre-initialized for the current row)the number of the current row rowNum)Implements org. ResultSet.
|
| context | back to summary |
|---|---|
| private final Map<?, ?> context
| |
| params | back to summary |
|---|---|
| private final Object[] params
| |
| RowMapperImpl | back to summary |
|---|---|
| public RowMapperImpl(Object[] parameters, Map<?, ?> context) Use an array results. More efficient if we know how many results to expect. | |
| mapRow | back to summary |
|---|---|
| public T mapRow(ResultSet rs, int rowNum) throws SQLException Implements org. Doc from org. Implementations must implement this method to map each row of data in the
| |