Java Reference
In-Depth Information
Listing 9-25.
package com.apress.springbatch.chapter9;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
public class CustomerItemPreparedStatementSetter implements
ItemPreparedStatementSetter<Customer> {
public void setValues(Customer customer, PreparedStatement ps)
throws SQLException {
ps.setString(1, customer.getFirstName());
ps.setString(2, customer.getMiddleInitial());
ps.setString(3, customer.getLastName());
ps.setString(4, customer.getAddress());
ps.setString(5, customer.getCity());
ps.setString(6, customer.getState());
ps.setString(7, customer.getZip());
As Listing 9-25 shows, there is no magic involved in setting the values for each PreparedStatement .
With this code, you can update formatJob 's configuration to write its output to the database. Listing 9-26
shows the configuration for the new ItemWriter.
Listing 9-26. jdbcBatchWriter 's Configuration
<beans:bean id="jdbcBatchWriter"
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="insert into customer (firstName,
middleInitial, lastName, address, city, state, zip) values (?, ?, ?, ?, ?, ?,
<beans:property name="itemPreparedStatementSetter"
<beans:bean id="preparedStatementSetter"
As you can see in Listing 9-26, the new jdbcBatchItemWriter references the dataSource bean from
the launch-context.xml file (the customer table is in the same schema as the Spring Batch tables you use
for the JobRepository). The SQL value is the same as the SQL statement you previously defined in Listing
9-23. The last dependency you provide is the reference to the CustomerItemPreparedStatementSetter .
The final piece of the puzzle to configure the new ItemWriter is to update the configuration for the
step to reference the new ItemWriter. To do this, all you need to do is update formatStep 's configuration
Search WWH ::

Custom Search