- Type Parameters:
- T- the result type
- All Implemented Interfaces:
- InitializingBean
- Direct Known Subclasses:
- GenericSqlQuery,- MappingSqlQueryWithParameters,- UpdatableSqlQuery
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.
- Author:
- Rod Johnson, Juergen Hoeller, Thomas Risberg, Yanming Zhou
- See Also:
- 
Field SummaryFields inherited from class org.springframework.jdbc.object.RdbmsOperationlogger
- 
Constructor SummaryConstructorsConstructorDescriptionSqlQuery()Constructor to allow use as a JavaBean.SqlQuery(DataSource ds, String sql) Convenient constructor with aDataSourceand SQL string.
- 
Method SummaryModifier and TypeMethodDescriptionexecute()Convenient method to execute without parameters nor context.execute(int p1) Convenient method to execute with a single int parameter.execute(int p1, int p2) Convenient method to execute with two int parameters.Convenient method to execute with two int parameters and context.Convenient method to execute with a single int parameter and context.execute(long p1) Convenient method to execute with a single long parameter.Convenient method to execute with a single long parameter and context.Convenient method to execute without context.Central execution method.Convenient method to execute with a single String parameter.Convenient method to execute with a single String parameter and context.Convenient method to execute without parameters.executeByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, ?> context) Central execution method.executeByNamedParam(Map<String, ? extends @Nullable Object> paramMap) Convenient method to execute without context.findObject(int p1) Convenient method to find a single object given a single int parameter.findObject(int p1, int p2) Convenient method to find a single object given two int parameters.findObject(int p1, int p2, @Nullable Map<?, ?> context) Convenient method to find a single object given two int parameters and a context.findObject(int p1, @Nullable Map<?, ?> context) Convenient method to find a single object given a single int parameter and a context.findObject(long p1) Convenient method to find a single object given a single long parameter.findObject(long p1, @Nullable Map<?, ?> context) Convenient method to find a single object given a single long parameter and a context.findObject(Object... params) Convenient method to find a single object without context.findObject(Object @Nullable [] params, @Nullable Map<?, ?> context) Generic object finder method, used by all otherfindObjectmethods.findObject(String p1) Convenient method to find a single object given a single String parameter.findObject(String p1, @Nullable Map<?, ?> context) Convenient method to find a single object given a single String parameter and a context.findObjectByNamedParam(Map<String, ?> paramMap) Convenient method to execute without context.findObjectByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, ?> context) Generic object finder method for named parameters.Subclasses must implement this method to extract an object per row, to be returned by theexecutemethod as an aggregatedList.stream()Convenient method to stream without parameters nor context.Convenient method to stream without context.Central stream method.Convenient method to stream without parameters.streamByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, ?> context) Central stream method.streamByNamedParam(Map<String, ? extends @Nullable Object> paramMap) Convenient method to stream without context.Methods inherited from class org.springframework.jdbc.object.SqlOperationcompileInternal, getParsedSql, newPreparedStatementCreator, newPreparedStatementCreator, newPreparedStatementSetter, onCompileInternalMethods inherited from class org.springframework.jdbc.object.RdbmsOperationafterPropertiesSet, 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
- 
Constructor Details- 
SqlQuerypublic SqlQuery()Constructor to allow use as a JavaBean.The DataSourceand SQL must be supplied before compilation and use.
- 
SqlQueryConvenient constructor with aDataSourceand SQL string.- Parameters:
- ds- the- DataSourceto use to get connections
- sql- the SQL to execute; SQL can also be supplied at runtime by overriding the- RdbmsOperation.getSql()method.
 
 
- 
- 
Method Details- 
executepublic List<T> execute(Object @Nullable [] params, @Nullable Map<?, ?> context) throws DataAccessExceptionCentral execution method. All un-named parameter execution goes through this method.- Parameters:
- params- parameters, similar to JDO query parameters. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
- context- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- Returns:
- 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.
- Throws:
- DataAccessException
 
- 
streampublic Stream<T> stream(Object @Nullable [] params, @Nullable Map<?, ?> context) throws DataAccessExceptionCentral stream method. All un-named parameter execution goes through this method.- Parameters:
- params- parameters, similar to JDO query parameters. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
- context- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- Returns:
- a result Stream 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.
- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
executeConvenient method to execute without context.- Parameters:
- params- parameters for the query. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
- Throws:
- DataAccessException
 
- 
streamConvenient method to stream without context.- Parameters:
- params- parameters for the query. Primitive parameters must be represented by their Object wrapper type. The ordering of parameters is significant.
- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
executeConvenient method to execute without parameters.- Parameters:
- context- the contextual information for object creation
- Throws:
- DataAccessException
 
- 
streamConvenient method to stream without parameters.- Parameters:
- context- the contextual information for object creation
- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
executeConvenient method to execute without parameters nor context.- Throws:
- DataAccessException
 
- 
streamConvenient method to stream without parameters nor context.- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
executeConvenient method to execute with a single int parameter and context.- Parameters:
- p1- single int parameter
- context- the contextual information for object creation
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with a single int parameter.- Parameters:
- p1- single int parameter
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with two int parameters and context.- Parameters:
- p1- first int parameter
- p2- second int parameter
- context- the contextual information for object creation
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with two int parameters.- Parameters:
- p1- first int parameter
- p2- second int parameter
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with a single long parameter and context.- Parameters:
- p1- single long parameter
- context- the contextual information for object creation
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with a single long parameter.- Parameters:
- p1- single long parameter
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with a single String parameter and context.- Parameters:
- p1- single String parameter
- context- the contextual information for object creation
- Throws:
- DataAccessException
 
- 
executeConvenient method to execute with a single String parameter.- Parameters:
- p1- single String parameter
- Throws:
- DataAccessException
 
- 
executeByNamedParampublic List<T> executeByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, throws DataAccessException?> context) Central execution method. All named parameter execution goes through this method.- Parameters:
- 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- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- Returns:
- 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.
- Throws:
- DataAccessException
 
- 
streamByNamedParampublic Stream<T> streamByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, throws DataAccessException?> context) Central stream method. All named parameter execution goes through this method.- Parameters:
- 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- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- Returns:
- a Stream 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.
- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
executeByNamedParampublic List<T> executeByNamedParam(Map<String, ? extends @Nullable Object> paramMap) throws DataAccessException Convenient method to execute without context.- Parameters:
- 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.
- Throws:
- DataAccessException
 
- 
streamByNamedParampublic Stream<T> streamByNamedParam(Map<String, ? extends @Nullable Object> paramMap) throws DataAccessException Convenient method to stream without context.- Parameters:
- 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.
- Throws:
- DataAccessException
- Since:
- 7.0
 
- 
findObjectpublic @Nullable T findObject(Object @Nullable [] params, @Nullable Map<?, ?> context) throws DataAccessExceptionGeneric object finder method, used by all otherfindObjectmethods. Object finder methods are like EJB entity bean finders, in that it is considered an error if they return more than one result.- Returns:
- the result object, or nullif not found. Subclasses may choose to treat this as an error and throw an exception.
- Throws:
- DataAccessException
- See Also:
 
- 
findObjectConvenient method to find a single object without context.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single int parameter and a context.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single int parameter.- Throws:
- DataAccessException
 
- 
findObjectpublic @Nullable T findObject(int p1, int p2, @Nullable Map<?, ?> context) throws DataAccessExceptionConvenient method to find a single object given two int parameters and a context.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given two int parameters.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single long parameter and a context.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single long parameter.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single String parameter and a context.- Throws:
- DataAccessException
 
- 
findObjectConvenient method to find a single object given a single String parameter.- Throws:
- DataAccessException
 
- 
findObjectByNamedParampublic @Nullable T findObjectByNamedParam(Map<String, ?> paramMap, @Nullable Map<?, throws DataAccessException?> context) Generic object finder method for named parameters.- Parameters:
- paramMap- a Map of parameter name to parameter object, matching named parameters specified in the SQL statement. Ordering is not significant.
- context- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- Returns:
- 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.
- Throws:
- DataAccessException
 
- 
findObjectByNamedParamConvenient method to execute without context.- Parameters:
- paramMap- a Map of parameter name to parameter object, matching named parameters specified in the SQL statement. Ordering is not significant.
- Throws:
- DataAccessException
 
- 
newRowMapperprotected abstract RowMapper<T> newRowMapper(@Nullable Object @Nullable [] parameters, @Nullable Map<?, ?> context) Subclasses must implement this method to extract an object per row, to be returned by theexecutemethod as an aggregatedList.- Parameters:
- parameters- the parameters to the- execute()method, in case subclass is interested; may be- nullif there were no parameters.
- context- the contextual information passed to the- mapRowcallback method. The JDBC operation itself doesn't rely on this parameter, but it can be useful for creating the objects of the result list.
- See Also:
 
 
-