Class JdbcBatchItemWriter<T>
- java.lang.Object
- org.springframework.batch.item.database.JdbcBatchItemWriter<T>
- All Implemented Interfaces:
ItemWriter<T>,org.springframework.beans.factory.InitializingBean
public class JdbcBatchItemWriter<T> extends java.lang.Object implements ItemWriter<T>, org.springframework.beans.factory.InitializingBean
The user must provide an SQL query and a special callback for either ofItemWriterthat uses the batching features fromNamedParameterJdbcTemplateto execute a batch of statements for all items provided.ItemPreparedStatementSetterorItemSqlParameterSourceProvider. You can use either named parameters or the traditional '?' placeholders. If you use the named parameter support then you should provide aItemSqlParameterSourceProvider, otherwise you should provide aItemPreparedStatementSetter. This callback would be responsible for mapping the item to the parameters needed to execute the SQL statement.
It is expected thatwrite(List)is called inside a transaction.
The writer is thread-safe after its properties are set (normal singleton behavior), so it can be used to write in multiple concurrent transactions.- Since:
- 2.0
- Author:
- Dave Syer, Thomas Risberg, Michael Minella
Field Summary
Fields Modifier and Type Field Description protected static org.apache.commons.logging.Loglogger
Constructor Summary
Constructors Constructor Description JdbcBatchItemWriter()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a parameter source.voidsetAssertUpdates(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.voidsetDataSource(javax.sql.DataSource dataSource)Public setter for the data source for injection purposes.voidsetItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)Public setter for theItemPreparedStatementSetter.voidsetItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)Public setter for theItemSqlParameterSourceProvider.voidsetJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate)Public setter for theNamedParameterJdbcOperations.voidsetSql(java.lang.String sql)Public setter for the query string to execute on write.voidwrite(java.util.List<? extends T> items)Process the supplied data element.
Method Detail
setAssertUpdates
public 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.- Parameters:
assertUpdates- the flag to set. Defaults to true;
setSql
public void setSql(java.lang.String sql)
Public setter for the query string to execute on write. The parameters should correspond to those known to theItemPreparedStatementSetter.- Parameters:
sql- the query to set
setItemPreparedStatementSetter
public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter)
Public setter for theItemPreparedStatementSetter.- Parameters:
preparedStatementSetter- theItemPreparedStatementSetterto set. This is required when using traditional '?' placeholders for the SQL statement.
setItemSqlParameterSourceProvider
public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider)
Public setter for theItemSqlParameterSourceProvider.- Parameters:
itemSqlParameterSourceProvider- theItemSqlParameterSourceProviderto set. This is required when using named parameters for the SQL statement and the type to be written does not implementMap.
setDataSource
public void setDataSource(javax.sql.DataSource dataSource)
Public setter for the data source for injection purposes.- Parameters:
dataSource-DataSourceto use for querying against
setJdbcTemplate
public void setJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations namedParameterJdbcTemplate)
Public setter for theNamedParameterJdbcOperations.- Parameters:
namedParameterJdbcTemplate- theNamedParameterJdbcOperationsto set
afterPropertiesSet
public void afterPropertiesSet()
Check mandatory properties - there must be a SimpleJdbcTemplate and an SQL statement plus a parameter source.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
write
public void write(java.util.List<? extends T> items) throws java.lang.Exception
Description copied from interface:ItemWriterProcess the supplied data element. Will not be called with any null items in normal operation.- Specified by:
writein interfaceItemWriter<T>- Parameters:
items- items to be written- Throws:
java.lang.Exception- if there are errors. The framework will catch the exception and convert or rethrow it as appropriate.