public class JdbcBatchItemWriter<T> extends java.lang.Object implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean
ItemWriter
that uses the batching features from
NamedParameterJdbcTemplate
to execute a batch of statements for all items
provided.
ItemPreparedStatementSetter
or ItemSqlParameterSourceProvider
.
You can use either named parameters or the traditional '?' placeholders. If you use the
named parameter support then you should provide a ItemSqlParameterSourceProvider
,
otherwise you should provide a ItemPreparedStatementSetter
.
This callback would be responsible for mapping the item to the parameters needed to
execute the SQL statement.write(List)
is called inside a transaction.Modifier and Type | Field and Description |
---|---|
protected boolean |
assertUpdates |
protected ItemPreparedStatementSetter<T> |
itemPreparedStatementSetter |
protected ItemSqlParameterSourceProvider<T> |
itemSqlParameterSourceProvider |
protected static org.apache.commons.logging.Log |
logger |
protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations |
namedParameterJdbcTemplate |
protected int |
parameterCount |
protected java.lang.String |
sql |
protected boolean |
usingNamedParameters |
Constructor and Description |
---|
JdbcBatchItemWriter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a
parameter source.
|
void |
setAssertUpdates(boolean assertUpdates)
Public setter for the flag that determines whether an assertion is made
that all items cause at least one row to be updated.
|
void |
setDataSource(javax.sql.DataSource dataSource)
Public setter for the data source for injection purposes.
|
void |
setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
Public setter for the
ItemPreparedStatementSetter . |
void |
setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)
Public setter for the
ItemSqlParameterSourceProvider . |
void |
setJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate)
Public setter for the
NamedParameterJdbcOperations . |
void |
setSql(java.lang.String sql)
Public setter for the query string to execute on write.
|
void |
write(java.util.List<? extends T> items)
Process the supplied data element.
|
protected static final org.apache.commons.logging.Log logger
protected org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate
protected ItemPreparedStatementSetter<T> itemPreparedStatementSetter
protected ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider
protected java.lang.String sql
protected boolean assertUpdates
protected int parameterCount
protected boolean usingNamedParameters
public void setAssertUpdates(boolean assertUpdates)
assertUpdates
- the flag to set. Defaults to true;public void setSql(java.lang.String sql)
ItemPreparedStatementSetter
.sql
- the query to setpublic void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
ItemPreparedStatementSetter
.preparedStatementSetter
- the ItemPreparedStatementSetter
to
set. This is required when using traditional '?' placeholders for the SQL statement.public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)
ItemSqlParameterSourceProvider
.itemSqlParameterSourceProvider
- the ItemSqlParameterSourceProvider
to
set. This is required when using named parameters for the SQL statement and the type
to be written does not implement Map
.public void setDataSource(javax.sql.DataSource dataSource)
dataSource
- DataSource
to use for querying againstpublic void setJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate)
NamedParameterJdbcOperations
.namedParameterJdbcTemplate
- the NamedParameterJdbcOperations
to setpublic void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
public void write(java.util.List<? extends T> items) throws java.lang.Exception
ItemWriter
write
in interface ItemWriter<T>
items
- items to be writtenjava.lang.Exception
- if there are errors. The framework will catch the
exception and convert or rethrow it as appropriate.