Java Reference
In-Depth Information
Figure 9-7. Customer table
To write to the database, you use JdbcBatchItemWriter as you did earlier in this chapter. If you
remember, JdbcBatchItemWriter depends on three things: a datasource, a prepared statement, and an
implementation of the ItemPreparedStatementSetter interface to populate the prepared statement with
values from the Customer item. Listing 9-54 has the code for CustomerItemPreparedStatementSetter .
Listing 9-54. CustomerItemPreparedStatementSetter.java
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());
ps.setString(8, customer.getEmail());
}
}
On the flip side, after you've imported the Customer items into the database, you need to read them
out again in the second step. For this step, you use the JdbcCursorItemReader discussed back in Chapter
7. Like JdbcBatchItemWriter , JdbcCursorItemReader also depends on a datasource. However, this
ItemReader only needs an SQL statement instead of a prepared statement, and it needs a RowMapper
implementation to map the returned ResultSet into items you can process. The CustomerRowMapper
implementation is shown in Listing 9-55.
Listing 9-55. CustomerRowMapper.java
package com.apress.springbatch.chapter9;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class CustomerRowMapper implements RowMapper<Customer> {
 
Search WWH ::




Custom Search