Top Description Fields Constructors Methods
org.springframework.jdbc.core.namedparam

public Class NamedParameterJdbcTemplate

extends Object
implements NamedParameterJdbcOperations
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
Imports
java.sql.PreparedStatement, .SQLException, java.util.List, .Map, java.util.function.Consumer, java.util.stream.Stream, javax.sql.DataSource, org.springframework.dao.DataAccessException, org.springframework.dao.support.DataAccessUtils, org.springframework.jdbc.core.BatchPreparedStatementSetter, .ColumnMapRowMapper, .JdbcOperations, .JdbcTemplate, .PreparedStatementCallback, .PreparedStatementCreator, .PreparedStatementCreatorFactory, .ResultSetExtractor, .RowCallbackHandler, .RowMapper, .SingleColumnRowMapper, .SqlParameter, .SqlRowSetResultSetExtractor, org.springframework.jdbc.support.KeyHolder, org.springframework.jdbc.support.rowset.SqlRowSet, org.springframework.lang.Nullable, org.springframework.util.Assert, .ConcurrentLruCache

Template class with a basic set of JDBC operations, allowing the use of named parameters rather than traditional '?' placeholders.

This class delegates to a wrapped JdbcTemplate once the substitution from named parameters to JDBC style '?' placeholders is done at execution time. It also allows for expanding a java.util.List of values to the appropriate number of placeholders.

An instance of this template class is thread-safe once configured. The underlying org.springframework.jdbc.core.JdbcTemplate is exposed to allow for convenient access to the traditional org.springframework.jdbc.core.JdbcTemplate methods.

NOTE: As of 6.1, there is a unified JDBC access facade available in the form of org.springframework.jdbc.core.simple.JdbcClient. JdbcClient provides a fluent API style for common JDBC queries/updates with flexible use of indexed or named parameters. It delegates to a JdbcTemplate/NamedParameterJdbcTemplate for actual execution.

Authors
Thomas Risberg, Juergen Hoeller
Since
2.0
See Also
NamedParameterJdbcOperations, SqlParameterSource, ResultSetExtractor, RowCallbackHandler, RowMapper, org.springframework.jdbc.core.JdbcTemplate

Field Summary

Modifier and TypeField and Description
private final JdbcOperations
classicJdbcTemplate

The JdbcTemplate we are wrapping.

public static final int
DEFAULT_CACHE_LIMIT

Default maximum number of entries for this template's SQL cache: 256.

private volatile ConcurrentLruCache<String, ParsedSql>
parsedSqlCache

Cache of original SQL String to ParsedSql representation.

Constructor Summary

AccessConstructor and Description
public
NamedParameterJdbcTemplate(DataSource
the JDBC DataSource to access
dataSource
)

Create a new NamedParameterJdbcTemplate for the given DataSource.

public
NamedParameterJdbcTemplate(JdbcOperations
the classic Spring JdbcTemplate to wrap
classicJdbcTemplate
)

Create a new NamedParameterJdbcTemplate for the given classic Spring org.springframework.jdbc.core.JdbcTemplate.

Method Summary

Modifier and TypeMethod and Description
public int[]
batchUpdate(String
the SQL statement to execute
sql
,
SqlParameterSource[]
the array of SqlParameterSource containing the batch of arguments for the query
batchArgs
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.batchUpdate.

Execute a batch using the supplied SQL statement with the batch of supplied arguments.

public int[]
batchUpdate(String
the SQL statement to execute
sql
,
Map<String, ?>[]
the array of Maps containing the batch of arguments for the query
batchValues
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.batchUpdate.

Executes a batch using the supplied SQL statement with the batch of supplied arguments.

public int[]
batchUpdate(String
the SQL statement to execute
sql
,
SqlParameterSource[]
the array of SqlParameterSource containing the batch of arguments for the query
batchArgs
,
KeyHolder
a KeyHolder that will hold the generated keys
generatedKeyHolder
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.batchUpdate.

Execute a batch using the supplied SQL statement with the batch of supplied arguments, returning generated keys.

public int[]
batchUpdate(String
the SQL statement to execute
sql
,
SqlParameterSource[]
the array of SqlParameterSource containing the batch of arguments for the query
batchArgs
,
KeyHolder
a KeyHolder that will hold the generated keys
generatedKeyHolder
,
String[]
names of the columns that will have keys generated for them
keyColumnNames
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.batchUpdate.

Execute a batch using the supplied SQL statement with the batch of supplied arguments, returning generated keys.

public <T> T
execute(String
the SQL to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
PreparedStatementCallback<T>
callback object that specifies the action
action
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.execute.

Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.

public <T> T
execute(String
the SQL to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
PreparedStatementCallback<T>
callback object that specifies the action
action
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.execute.

Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.

public <T> T
execute(String
the SQL to execute
sql
,
PreparedStatementCallback<T>
callback object that specifies the action
action
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.execute.

Execute a JDBC data access operation, implemented as callback action working on a JDBC PreparedStatement.

public int
getCacheLimit()

Return the maximum number of entries for this template's SQL cache.

public JdbcOperations
getJdbcOperations()

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.getJdbcOperations.

Expose the classic Spring JdbcTemplate operations to allow invocation of less commonly used methods.

public JdbcTemplate
getJdbcTemplate()

Expose the classic Spring JdbcTemplate itself, if available, in particular for passing it on to other JdbcTemplate consumers.

protected ParsedSql

Returns:

a representation of the parsed SQL statement
getParsedSql
(String
the original SQL statement
sql
)

Obtain a parsed representation of the given SQL statement.

protected PreparedStatementCreator

Returns:

the corresponding PreparedStatementCreator
getPreparedStatementCreator
(String
the SQL statement to execute
sql
,
SqlParameterSource
container of arguments to bind
paramSource
)

Build a PreparedStatementCreator based on the given SQL and named parameters.

protected PreparedStatementCreator

Returns:

the corresponding PreparedStatementCreator
getPreparedStatementCreator
(String
the SQL statement to execute
sql
,
SqlParameterSource
container of arguments to bind
paramSource
,
Consumer<PreparedStatementCreatorFactory>
callback for setting further properties on the PreparedStatementCreatorFactory in use, applied before the actual newPreparedStatementCreator call
customizer
)

Build a PreparedStatementCreator based on the given SQL and named parameters.

protected PreparedStatementCreatorFactory

Returns:

the corresponding PreparedStatementCreatorFactory
getPreparedStatementCreatorFactory
(ParsedSql
parsed representation of the given SQL statement
parsedSql
,
SqlParameterSource
container of arguments to bind
paramSource
)

Build a PreparedStatementCreatorFactory based on the given SQL and named parameters.

public <T> T
query(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
ResultSetExtractor<T>
object that will extract results
rse
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet with a ResultSetExtractor.

public <T> T
query(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
ResultSetExtractor<T>
object that will extract results
rse
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet with a ResultSetExtractor.

public <T> T
query(String
the SQL query to execute
sql
,
ResultSetExtractor<T>
object that will extract results
rse
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL, reading the ResultSet with a ResultSetExtractor.

public void
query(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
RowCallbackHandler
object that will extract results, one row at a time
rch
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler.

public void
query(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
RowCallbackHandler
object that will extract results, one row at a time
rch
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, reading the ResultSet on a per-row basis with a RowCallbackHandler.

public void
query(String
the SQL query to execute
sql
,
RowCallbackHandler
object that will extract results, one row at a time
rch
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL, reading the ResultSet on a per-row basis with a RowCallbackHandler.

public <T> List<T>
query(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.

public <T> List<T>
query(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper.

public <T> List<T>
query(String
the SQL query to execute
sql
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.query.

Query given SQL to create a prepared statement from SQL, mapping each row to a Java object via a RowMapper.

public <T> List<T>
queryForList(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
Class<T>
the required type of element in the result list (for example, Integer.class)
elementType
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForList.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

public <T> List<T>
queryForList(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
Class<T>
the required type of element in the result list (for example, Integer.class)
elementType
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForList.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

public List<Map<String, Object>>
queryForList(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForList.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

public List<Map<String, Object>>
queryForList(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForList.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list.

public Map<String, Object>
queryForMap(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForMap.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result Map.

public Map<String, Object>
queryForMap(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForMap.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result Map.

public <T> T
queryForObject(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForObject.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping a single result row to a Java object via a RowMapper.

public <T> T
queryForObject(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForObject.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping a single result row to a Java object via a RowMapper.

public <T> T
queryForObject(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
Class<T>
the type that the result object is expected to match
requiredType
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForObject.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.

public <T> T
queryForObject(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
Class<T>
the type that the result object is expected to match
requiredType
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForObject.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object.

public SqlRowSet
queryForRowSet(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForRowSet.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting an SqlRowSet.

public SqlRowSet
queryForRowSet(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForRowSet.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting an SqlRowSet.

public <T> Stream<T>
queryForStream(String
the SQL query to execute
sql
,
SqlParameterSource
container of arguments to bind to the query
paramSource
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForStream.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper, and turning it into an iterable and closeable Stream.

public <T> Stream<T>
queryForStream(String
the SQL query to execute
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
,
RowMapper<T>
object that will map one object per row
rowMapper
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.queryForStream.

Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, mapping each row to a Java object via a RowMapper, and turning it into an iterable and closeable Stream.

public void
setCacheLimit(int cacheLimit)

Specify the maximum number of entries for this template's SQL cache.

public int
update(String
the SQL containing named parameters
sql
,
SqlParameterSource
container of arguments and SQL types to bind to the query
paramSource
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.update.

Issue an update via a prepared statement, binding the given arguments.

public int
update(String
the SQL containing named parameters
sql
,
Map<String, ?>
map of parameters to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type)
paramMap
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.update.

Issue an update via a prepared statement, binding the given arguments.

public int
update(String
the SQL containing named parameters
sql
,
SqlParameterSource
container of arguments and SQL types to bind to the query
paramSource
,
KeyHolder
a KeyHolder that will hold the generated keys
generatedKeyHolder
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.update.

Issue an update via a prepared statement, binding the given arguments, returning generated keys.

public int
update(String
the SQL containing named parameters
sql
,
SqlParameterSource
container of arguments and SQL types to bind to the query
paramSource
,
KeyHolder
a KeyHolder that will hold the generated keys
generatedKeyHolder
,
String[]
names of the columns that will have keys generated for them
keyColumnNames
)

Implements org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations.update.

Issue an update via a prepared statement, binding the given arguments, returning generated keys.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait