T
- the result typepublic abstract class SqlQuery<T> extends SqlOperation
Subclasses must implement the newRowMapper(java.lang.Object[], java.util.Map<?, ?>)
method to provide
an object that can extract the results of iterating over the
ResultSet
created during the execution of the query.
This class provides a number of public execute
methods that are
analogous to the different convenient JDO query execute methods. Subclasses
can either rely on one of these inherited methods, or can add their own
custom execution methods, with meaningful names and typed parameters
(definitely a best practice). Each custom query method will invoke one of
this class's untyped query methods.
Like all RdbmsOperation
classes that ship with the Spring
Framework, SqlQuery
instances are thread-safe after their
initialization is complete. That is, after they are constructed and configured
via their setter methods, they can be used safely from multiple threads.
SqlUpdate
logger
Constructor and Description |
---|
SqlQuery()
Constructor to allow use as a JavaBean.
|
SqlQuery(javax.sql.DataSource ds,
java.lang.String sql)
Convenient constructor with a
DataSource and SQL string. |
Modifier and Type | Method and Description |
---|---|
java.util.List<T> |
execute()
Convenient method to execute without parameters nor context.
|
java.util.List<T> |
execute(int p1)
Convenient method to execute with a single int parameter.
|
java.util.List<T> |
execute(int p1,
int p2)
Convenient method to execute with two int parameters.
|
java.util.List<T> |
execute(int p1,
int p2,
java.util.Map<?,?> context)
Convenient method to execute with two int parameters and context.
|
java.util.List<T> |
execute(int p1,
java.util.Map<?,?> context)
Convenient method to execute with a single int parameter and context.
|
java.util.List<T> |
execute(long p1)
Convenient method to execute with a single long parameter.
|
java.util.List<T> |
execute(long p1,
java.util.Map<?,?> context)
Convenient method to execute with a single long parameter and context.
|
java.util.List<T> |
execute(java.util.Map<?,?> context)
Convenient method to execute without parameters.
|
java.util.List<T> |
execute(java.lang.Object... params)
Convenient method to execute without context.
|
java.util.List<T> |
execute(java.lang.Object[] params,
java.util.Map<?,?> context)
Central execution method.
|
java.util.List<T> |
execute(java.lang.String p1)
Convenient method to execute with a single String parameter.
|
java.util.List<T> |
execute(java.lang.String p1,
java.util.Map<?,?> context)
Convenient method to execute with a single String parameter and context.
|
java.util.List<T> |
executeByNamedParam(java.util.Map<java.lang.String,?> paramMap)
Convenient method to execute without context.
|
java.util.List<T> |
executeByNamedParam(java.util.Map<java.lang.String,?> paramMap,
java.util.Map<?,?> context)
Central execution method.
|
T |
findObject(int p1)
Convenient method to find a single object given a single int parameter.
|
T |
findObject(int p1,
int p2)
Convenient method to find a single object given two int parameters.
|
T |
findObject(int p1,
int p2,
java.util.Map<?,?> context)
Convenient method to find a single object given two int parameters
and a context.
|
T |
findObject(int p1,
java.util.Map<?,?> context)
Convenient method to find a single object given a single int parameter
and a context.
|
T |
findObject(long p1)
Convenient method to find a single object given a single long parameter.
|
T |
findObject(long p1,
java.util.Map<?,?> context)
Convenient method to find a single object given a single long parameter
and a context.
|
T |
findObject(java.lang.Object... params)
Convenient method to find a single object without context.
|
T |
findObject(java.lang.Object[] params,
java.util.Map<?,?> context)
Generic object finder method, used by all other
findObject methods. |
T |
findObject(java.lang.String p1)
Convenient method to find a single object given a single String parameter.
|
T |
findObject(java.lang.String p1,
java.util.Map<?,?> context)
Convenient method to find a single object given a single String parameter
and a context.
|
T |
findObjectByNamedParam(java.util.Map<java.lang.String,?> paramMap)
Convenient method to execute without context.
|
T |
findObjectByNamedParam(java.util.Map<java.lang.String,?> paramMap,
java.util.Map<?,?> context)
Generic object finder method for named parameters.
|
int |
getRowsExpected()
Get the number of rows expected.
|
protected abstract RowMapper<T> |
newRowMapper(java.lang.Object[] parameters,
java.util.Map<?,?> context)
Subclasses must implement this method to extract an object per row, to be
returned by the
execute method as an aggregated List . |
void |
setRowsExpected(int rowsExpected)
Set the number of rows expected.
|
compileInternal, getParsedSql, newPreparedStatementCreator, newPreparedStatementCreator, newPreparedStatementSetter, onCompileInternal
afterPropertiesSet, allowsUnusedParameters, checkCompiled, compile, declareParameter, getDeclaredParameters, getGeneratedKeysColumnNames, getJdbcTemplate, getResultSetType, getSql, isCompiled, isReturnGeneratedKeys, isUpdatableResults, resolveSql, setDataSource, setFetchSize, setGeneratedKeysColumnNames, setJdbcTemplate, setMaxRows, setParameters, setQueryTimeout, setResultSetType, setReturnGeneratedKeys, setSql, setTypes, setUpdatableResults, supportsLobParameters, validateNamedParameters, validateParameters
public SqlQuery()
The DataSource
and SQL must be supplied before
compilation and use.
public SqlQuery(javax.sql.DataSource ds, java.lang.String sql)
DataSource
and SQL string.ds
- the DataSource
to use to get connectionssql
- the SQL to execute; SQL can also be supplied at runtime
by overriding the RdbmsOperation.getSql()
method.public void setRowsExpected(int rowsExpected)
This can be used to ensure efficient storage of results. The default behavior is not to expect any specific number of rows.
public int getRowsExpected()
public java.util.List<T> execute(@Nullable java.lang.Object[] params, @Nullable java.util.Map<?,?> context) throws DataAccessException
params
- parameters, similar to JDO query parameters.
Primitive parameters must be represented by their Object wrapper type.
The ordering of parameters is significant.context
- 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.DataAccessException
public java.util.List<T> execute(java.lang.Object... params) throws DataAccessException
params
- parameters for the query. Primitive parameters must
be represented by their Object wrapper type. The ordering of parameters is
significant.DataAccessException
public java.util.List<T> execute(java.util.Map<?,?> context) throws DataAccessException
context
- the contextual information for object creationDataAccessException
public java.util.List<T> execute() throws DataAccessException
DataAccessException
public java.util.List<T> execute(int p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
p1
- single int parametercontext
- the contextual information for object creationDataAccessException
public java.util.List<T> execute(int p1) throws DataAccessException
p1
- single int parameterDataAccessException
public java.util.List<T> execute(int p1, int p2, @Nullable java.util.Map<?,?> context) throws DataAccessException
p1
- first int parameterp2
- second int parametercontext
- the contextual information for object creationDataAccessException
public java.util.List<T> execute(int p1, int p2) throws DataAccessException
p1
- first int parameterp2
- second int parameterDataAccessException
public java.util.List<T> execute(long p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
p1
- single long parametercontext
- the contextual information for object creationDataAccessException
public java.util.List<T> execute(long p1) throws DataAccessException
p1
- single long parameterDataAccessException
public java.util.List<T> execute(java.lang.String p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
p1
- single String parametercontext
- the contextual information for object creationDataAccessException
public java.util.List<T> execute(java.lang.String p1) throws DataAccessException
p1
- single String parameterDataAccessException
public java.util.List<T> executeByNamedParam(java.util.Map<java.lang.String,?> paramMap, @Nullable java.util.Map<?,?> context) throws DataAccessException
paramMap
- parameters associated with the name specified while declaring
the SqlParameters. Primitive parameters must be represented by their Object wrapper
type. The ordering of parameters is not significant since they are supplied in a
SqlParameterMap which is an implementation of the Map interface.context
- 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.DataAccessException
public java.util.List<T> executeByNamedParam(java.util.Map<java.lang.String,?> paramMap) throws DataAccessException
paramMap
- parameters associated with the name specified while declaring
the SqlParameters. Primitive parameters must be represented by their Object wrapper
type. The ordering of parameters is not significant.DataAccessException
@Nullable public T findObject(@Nullable java.lang.Object[] params, @Nullable java.util.Map<?,?> context) throws DataAccessException
findObject
methods.
Object finder methods are like EJB entity bean finders, in that it is
considered an error if they return more than one result.null
if not found. Subclasses may
choose to treat this as an error and throw an exception.DataAccessException
DataAccessUtils.singleResult(java.util.Collection<T>)
@Nullable public T findObject(java.lang.Object... params) throws DataAccessException
DataAccessException
@Nullable public T findObject(int p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
DataAccessException
@Nullable public T findObject(int p1) throws DataAccessException
DataAccessException
@Nullable public T findObject(int p1, int p2, @Nullable java.util.Map<?,?> context) throws DataAccessException
DataAccessException
@Nullable public T findObject(int p1, int p2) throws DataAccessException
DataAccessException
@Nullable public T findObject(long p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
DataAccessException
@Nullable public T findObject(long p1) throws DataAccessException
DataAccessException
@Nullable public T findObject(java.lang.String p1, @Nullable java.util.Map<?,?> context) throws DataAccessException
DataAccessException
@Nullable public T findObject(java.lang.String p1) throws DataAccessException
DataAccessException
@Nullable public T findObjectByNamedParam(java.util.Map<java.lang.String,?> paramMap, @Nullable java.util.Map<?,?> context) throws DataAccessException
paramMap
- a Map of parameter name to parameter object,
matching named parameters specified in the SQL statement.
Ordering is not significant.context
- 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.DataAccessException
@Nullable public T findObjectByNamedParam(java.util.Map<java.lang.String,?> paramMap) throws DataAccessException
paramMap
- a Map of parameter name to parameter object,
matching named parameters specified in the SQL statement.
Ordering is not significant.DataAccessException
protected abstract RowMapper<T> newRowMapper(@Nullable java.lang.Object[] parameters, @Nullable java.util.Map<?,?> context)
execute
method as an aggregated List
.parameters
- the parameters to the execute()
method,
in case subclass is interested; may be null
if there
were no parameters.context
- 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.execute(java.lang.Object[], java.util.Map<?, ?>)