类 SqlQuery<T>
- java.lang.Object
- org.springframework.jdbc.object.RdbmsOperation
- org.springframework.jdbc.object.SqlOperation
- org.springframework.jdbc.object.SqlQuery<T>
- 所有已实现的接口:
InitializingBean
public abstract class SqlQuery<T> extends SqlOperation
Reusable operation object representing a SQL query.Subclasses must implement the
newRowMapper(java.lang.Object[], java.util.Map<?, ?>)
method to provide an object that can extract the results of iterating over theResultSet
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.- 作者:
- Rod Johnson, Juergen Hoeller, Thomas Risberg
- 另请参阅:
SqlUpdate
字段概要
从类继承的字段 org.springframework.jdbc.object.RdbmsOperation
logger
构造器概要
构造器 构造器 说明 SqlQuery()
Constructor to allow use as a JavaBean.SqlQuery(DataSource ds, String sql)
Convenient constructor with aDataSource
and SQL string.
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 List<T>
execute()
Convenient method to execute without parameters nor context.List<T>
execute(int p1)
Convenient method to execute with a single int parameter.List<T>
execute(int p1, int p2)
Convenient method to execute with two int parameters.List<T>
execute(int p1, int p2, Map<?,?> context)
Convenient method to execute with two int parameters and context.List<T>
execute(int p1, Map<?,?> context)
Convenient method to execute with a single int parameter and context.List<T>
execute(long p1)
Convenient method to execute with a single long parameter.List<T>
execute(long p1, Map<?,?> context)
Convenient method to execute with a single long parameter and context.List<T>
execute(Object... params)
Convenient method to execute without context.List<T>
execute(Object[] params, Map<?,?> context)
Central execution method.List<T>
execute(String p1)
Convenient method to execute with a single String parameter.List<T>
execute(String p1, Map<?,?> context)
Convenient method to execute with a single String parameter and context.List<T>
execute(Map<?,?> context)
Convenient method to execute without parameters.List<T>
executeByNamedParam(Map<String,?> paramMap)
Convenient method to execute without context.List<T>
executeByNamedParam(Map<String,?> paramMap, 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, Map<?,?> context)
Convenient method to find a single object given two int parameters and a context.T
findObject(int p1, 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, Map<?,?> context)
Convenient method to find a single object given a single long parameter and a context.T
findObject(Object... params)
Convenient method to find a single object without context.T
findObject(Object[] params, Map<?,?> context)
Generic object finder method, used by all otherfindObject
methods.T
findObject(String p1)
Convenient method to find a single object given a single String parameter.T
findObject(String p1, Map<?,?> context)
Convenient method to find a single object given a single String parameter and a context.T
findObjectByNamedParam(Map<String,?> paramMap)
Convenient method to execute without context.T
findObjectByNamedParam(Map<String,?> paramMap, Map<?,?> context)
Generic object finder method for named parameters.int
getRowsExpected()
Get the number of rows expected.protected abstract RowMapper<T>
newRowMapper(Object[] parameters, Map<?,?> context)
Subclasses must implement this method to extract an object per row, to be returned by theexecute method as an aggregated List
.void
setRowsExpected(int rowsExpected)
Set the number of rows expected.从类继承的方法 org.springframework.jdbc.object.SqlOperation
compileInternal, getParsedSql, newPreparedStatementCreator, newPreparedStatementCreator, newPreparedStatementSetter, onCompileInternal
从类继承的方法 org.springframework.jdbc.object.RdbmsOperation
afterPropertiesSet, allowsUnusedParameters, checkCompiled, compile, declareParameter, getDeclaredParameters, getGeneratedKeysColumnNames, getJdbcTemplate, getResultSetType, getSql, isCompiled, isReturnGeneratedKeys, isUpdatableResults, setDataSource, setFetchSize, setGeneratedKeysColumnNames, setJdbcTemplate, setMaxRows, setParameters, setQueryTimeout, setResultSetType, setReturnGeneratedKeys, setSql, setTypes, setUpdatableResults, supportsLobParameters, validateNamedParameters, validateParameters
构造器详细资料
SqlQuery
public SqlQuery()
Constructor to allow use as a JavaBean.The
DataSource
and SQL must be supplied before compilation and use.
SqlQuery
public SqlQuery(DataSource ds, String sql)
Convenient constructor with aDataSource
and SQL string.- 参数:
ds
- theDataSource
to use to get connectionssql
- the SQL to execute; SQL can also be supplied at runtime by overriding theRdbmsOperation.getSql()
method.
方法详细资料
setRowsExpected
public void setRowsExpected(int rowsExpected)
Set the number of rows expected.This can be used to ensure efficient storage of results. The default behavior is not to expect any specific number of rows.
getRowsExpected
public int getRowsExpected()
Get the number of rows expected.
execute
public List<T> execute(Object[] params, Map<?,?> context) throws DataAccessException
Central execution method. All un-named parameter execution goes through this method.- 参数:
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 themapRow
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.- 返回:
- a List of objects, one per row of the ResultSet. Normally all these will be of the same class, although it is possible to use different types.
- 抛出:
DataAccessException
execute
public List<T> execute(Object... params) throws DataAccessException
Convenient method to execute without context.- 参数:
params
- parameters for the query. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.- 抛出:
DataAccessException
execute
public List<T> execute(Map<?,?> context) throws DataAccessException
Convenient method to execute without parameters.- 参数:
context
- the contextual information for object creation- 抛出:
DataAccessException
execute
public List<T> execute() throws DataAccessException
Convenient method to execute without parameters nor context.
execute
public List<T> execute(int p1, Map<?,?> context) throws DataAccessException
Convenient method to execute with a single int parameter and context.- 参数:
p1
- single int parametercontext
- the contextual information for object creation- 抛出:
DataAccessException
execute
public List<T> execute(int p1) throws DataAccessException
Convenient method to execute with a single int parameter.- 参数:
p1
- single int parameter- 抛出:
DataAccessException
execute
public List<T> execute(int p1, int p2, Map<?,?> context) throws DataAccessException
Convenient method to execute with two int parameters and context.- 参数:
p1
- first int parameterp2
- second int parametercontext
- the contextual information for object creation- 抛出:
DataAccessException
execute
public List<T> execute(int p1, int p2) throws DataAccessException
Convenient method to execute with two int parameters.- 参数:
p1
- first int parameterp2
- second int parameter- 抛出:
DataAccessException
execute
public List<T> execute(long p1, Map<?,?> context) throws DataAccessException
Convenient method to execute with a single long parameter and context.- 参数:
p1
- single long parametercontext
- the contextual information for object creation- 抛出:
DataAccessException
execute
public List<T> execute(long p1) throws DataAccessException
Convenient method to execute with a single long parameter.- 参数:
p1
- single long parameter- 抛出:
DataAccessException
execute
public List<T> execute(String p1, Map<?,?> context) throws DataAccessException
Convenient method to execute with a single String parameter and context.- 参数:
p1
- single String parametercontext
- the contextual information for object creation- 抛出:
DataAccessException
execute
public List<T> execute(String p1) throws DataAccessException
Convenient method to execute with a single String parameter.- 参数:
p1
- single String parameter- 抛出:
DataAccessException
executeByNamedParam
public List<T> executeByNamedParam(Map<String,?> paramMap, Map<?,?> context) throws DataAccessException
Central execution method. All named parameter execution goes through this method.- 参数:
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 themapRow
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.- 返回:
- a List of objects, one per row of the ResultSet. Normally all these will be of the same class, although it is possible to use different types.
- 抛出:
DataAccessException
executeByNamedParam
public List<T> executeByNamedParam(Map<String,?> paramMap) throws DataAccessException
Convenient method to execute without context.- 参数:
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
findObject
public T findObject(Object[] params, Map<?,?> context) throws DataAccessException
Generic object finder method, used by all otherfindObject
methods. Object finder methods are like EJB entity bean finders, in that it is considered an error if they return more than one result.- 返回:
- the result object, or
null
if not found. Subclasses may choose to treat this as an error and throw an exception. - 抛出:
DataAccessException
- 另请参阅:
DataAccessUtils.singleResult(java.util.Collection<T>)
findObject
public T findObject(Object... params) throws DataAccessException
Convenient method to find a single object without context.
findObject
public T findObject(int p1, Map<?,?> context) throws DataAccessException
Convenient method to find a single object given a single int parameter and a context.
findObject
public T findObject(int p1) throws DataAccessException
Convenient method to find a single object given a single int parameter.
findObject
public T findObject(int p1, int p2, Map<?,?> context) throws DataAccessException
Convenient method to find a single object given two int parameters and a context.
findObject
public T findObject(int p1, int p2) throws DataAccessException
Convenient method to find a single object given two int parameters.
findObject
public T findObject(long p1, Map<?,?> context) throws DataAccessException
Convenient method to find a single object given a single long parameter and a context.
findObject
public T findObject(long p1) throws DataAccessException
Convenient method to find a single object given a single long parameter.
findObject
public T findObject(String p1, Map<?,?> context) throws DataAccessException
Convenient method to find a single object given a single String parameter and a context.
findObject
public T findObject(String p1) throws DataAccessException
Convenient method to find a single object given a single String parameter.
findObjectByNamedParam
public T findObjectByNamedParam(Map<String,?> paramMap, Map<?,?> context) throws DataAccessException
Generic object finder method for named parameters.- 参数:
paramMap
- Map of parameter name to parameter object, matching named parameters specified in the SQL statement. Ordering is not significant.context
- contextual information passed to themapRow
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.- 返回:
- a List of objects, one per row of the ResultSet. Normally all these will be of the same class, although it is possible to use different types.
- 抛出:
DataAccessException
findObjectByNamedParam
public T findObjectByNamedParam(Map<String,?> paramMap) throws DataAccessException
Convenient method to execute without context.- 参数:
paramMap
- Map of parameter name to parameter object, matching named parameters specified in the SQL statement. Ordering is not significant.- 抛出:
DataAccessException
newRowMapper
protected abstract RowMapper<T> newRowMapper(Object[] parameters, Map<?,?> context)
Subclasses must implement this method to extract an object per row, to be returned by theexecute method as an aggregated List
.- 参数:
parameters
- the parameters to theexecute()
method, in case subclass is interested; may benull
if there were no parameters.context
- contextual information passed to themapRow
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<?, ?>)