Java Reference
In-Depth Information
dataSource
DataSource
null
(required)
Provides access to the required
database.
itemPreparedStatementSetter
ItemPreparedStatement
Setter
null
If a standard PreparedStatement
is provided (using ? for
parameters),
JdbcBatchItemWriter uses this
class to populate the parameter
values.
itemSqlParameterSourceProvi
der
ItemSqlParameter
SourceProvider
null
If named parameters are used in
the SQL provided, the
JdbcBatchItemWriter uses this
class to populate the parameter
values.
simpleJdbcTemplate
SimpleJdbcTemplate
null
Allows you to inject an
implementation of the
SimpleJdbcOperations interface.
sql
String
null
(required)
SQL to be executed for each item.
To use JdbcBatchItemWriter in formatJob , you replace xmlOutputWriter with a new jdbcBatchWriter
bean. Because you begin with a standard PreparedStatement syntax for the query (using question marks),
you need to provide it with a dataSource , the SQL to be executed, and an implementation of the
org.springframework.batch.item.database.ItemPreparedStatementSetter interface. Yes, you're correct
if you realized that you're going to have to write some code to make this one work.
ItemPreparedStatementSetter is a simple interface used to abstract the extraction of values from
each item and set them on the PreparedStatement . It contains a single method, as shown in Listing 9-24.
Listing 9-24. ItemPreparedStatementSetter Interface
package org.springframework.batch.item.database;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public interface ItemPreparedStatementSetter<T> {
void setValues(T item, PreparedStatement ps) throws SQLException;
}
To implement the ItemPreparedStatementSetter interface, you create your own
CustomerItemPreparedStatementSetter . This class implements the single setValues method that is
required by the ItemPreparedStatementSetter interface by using the normal PreparedStatement API to
populate each value of the PreparedStatement with the appropriate value from the item. Listing 9-25
shows the code for CustomerItemPreparedStatementSetter .
 
 
Search WWH ::




Custom Search