Top Description Inners Constructors Methods
org.springframework.jdbc.object

public abstract Class MappingSqlQueryWithParameters<T>

extends SqlQuery<T>
Class Inheritance
Known Direct Subclasses
org.springframework.jdbc.object.MappingSqlQuery
Type Parameters
<T>
the result type
Imports
java.sql.ResultSet, .SQLException, java.util.Map, javax.sql.DataSource, org.springframework.jdbc.core.RowMapper, org.springframework.lang.Nullable

Reusable RDBMS query in which concrete subclasses must implement the abstract mapRow(ResultSet, int) method to map each row of the JDBC ResultSet into an object.

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.

Authors
Rod Johnson, Thomas Risberg, Jean-Pierre Pawlak
See Also
org.springframework.jdbc.object.MappingSqlQuery, org.springframework.jdbc.object.SqlQuery

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
MappingSqlQueryWithParameters.RowMapperImpl

Implementation of RowMapper that calls the enclosing class's mapRow method for each row.

Constructor Summary

AccessConstructor and Description
public
MappingSqlQueryWithParameters()

Constructor to allow use as a JavaBean.

public
MappingSqlQueryWithParameters(DataSource
the DataSource to use to get connections
ds
,
String
the SQL to run
sql
)

Convenient constructor with DataSource and SQL string.

Method Summary

Modifier and TypeMethod and Description
protected abstract T

Returns:

an object of the result type
mapRow
(ResultSet
the 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 null if there are no parameters.
parameters
,
Map<?, ?>
passed to the execute() method. It can be null if no contextual information is need.
context
)

Subclasses must implement this method to convert each row of the ResultSet into an object of the result type.

protected RowMapper<T>
newRowMapper(Object[]
the parameters to the execute() method, in case subclass is interested; may be null if there were no parameters.
parameters
,
Map<?, ?>
the contextual information passed to the 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.
context
)

Implements abstract org.springframework.jdbc.object.SqlQuery.newRowMapper.

Implementation of protected abstract method.
Inherited from org.springframework.jdbc.object.SqlQuery:
executeexecuteexecuteexecuteexecuteexecuteexecuteexecuteexecuteexecuteexecuteexecuteexecuteByNamedParamexecuteByNamedParamfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectfindObjectByNamedParamfindObjectByNamedParamgetRowsExpectedsetRowsExpected

Constructor Detail

MappingSqlQueryWithParametersback to summary
public MappingSqlQueryWithParameters()

Constructor to allow use as a JavaBean.

MappingSqlQueryWithParametersback to summary
public MappingSqlQueryWithParameters(DataSource ds, String sql)

Convenient constructor with DataSource and SQL string.

Parameters
ds:DataSource

the DataSource to use to get connections

sql:String

the SQL to run

Method Detail

mapRowback 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.

Parameters
rs:ResultSet

the ResultSet we're working through

rowNum:int

row number (from 0) we're up to

parameters:Object[]

to the query (passed to the execute() method). Subclasses are rarely interested in these. It can be null if there are no parameters.

context:Map<?, ?>

passed to the execute() method. It can be null if no contextual information is need.

Returns:T

an object of the result type

Annotations
@Nullable
Exceptions
SQLException:
if there's an error extracting data. Subclasses can simply not catch SQLExceptions, relying on the framework to clean up.
newRowMapperback to summary
protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context)

Implements abstract org.springframework.jdbc.object.SqlQuery.newRowMapper.

Implementation of protected abstract method. This invokes the subclass's implementation of the mapRow() method.

Parameters
parameters:Object[]

Doc from org.springframework.jdbc.object.SqlQuery.newRowMapper.

the parameters to the execute() method, in case subclass is interested; may be null if there were no parameters.

context:Map<?, ?>

Doc from org.springframework.jdbc.object.SqlQuery.newRowMapper.

the contextual information passed to the 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.

Annotations
@Override
org.springframework.jdbc.object back to summary

protected Class MappingSqlQueryWithParameters.RowMapperImpl

extends Object
implements RowMapper<T>
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.object.MappingSqlQueryWithParameters.RowMapperImpl
All Implemented Interfaces
org.springframework.jdbc.core.RowMapper

Implementation of RowMapper that calls the enclosing class's mapRow method for each row.

Field Summary

Modifier and TypeField and Description
private final Map<?, ?>
private final Object[]

Constructor Summary

AccessConstructor and Description
public
RowMapperImpl(Object[] parameters, Map<?, ?> context)

Use an array results.

Method Summary

Modifier and TypeMethod and Description
public T
mapRow(ResultSet
the ResultSet to map (pre-initialized for the current row)
rs
,
int
the number of the current row
rowNum
)

Implements org.springframework.jdbc.core.RowMapper.mapRow.

Implementations must implement this method to map each row of data in the ResultSet.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contextback to summary
private final Map<?, ?> context
Annotations
@Nullable
paramsback to summary
private final Object[] params
Annotations
@Nullable

Constructor Detail

RowMapperImplback to summary
public RowMapperImpl(Object[] parameters, Map<?, ?> context)

Use an array results. More efficient if we know how many results to expect.

Method Detail

mapRowback to summary
public T mapRow(ResultSet rs, int rowNum) throws SQLException

Implements org.springframework.jdbc.core.RowMapper.mapRow.

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

Implementations must implement this method to map each row of data in the ResultSet. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

Parameters
rs:ResultSet

the ResultSet to map (pre-initialized for the current row)

rowNum:int

the number of the current row

Returns:T

the result object for the current row (may be null)

Annotations
@Override
@Nullable
Exceptions
SQLException:
if an SQLException is encountered while getting column values (that is, there's no need to catch SQLException)