Java Reference
In-Depth Information
Listing 7-37. CustomerRowMapper
package com.apress.springbatch.chapter7;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
public class CustomerRowMapper implements RowMapper {
public Customer mapRow(ResultSet resultSet, int rowNumber) throws
SQLException {
Customer customer = new Customer();
customer.setId(resultSet.getLong(“id”));
customer.setAddress(resultSet.getString("address"));
customer.setCity(resultSet.getString("city"));
customer.setFirstName(resultSet.getString("firstName"));
customer.setLastName(resultSet.getString("lastName"));
customer.setMiddleInitial(resultSet.getString("middleInitial"));
customer.setState(resultSet.getString("state"));
customer.setZip(resultSet.getString("zip"));
return customer;
}
}
With the ability to map your query results to a domain object, you need to be able to execute a query
by opening a cursor to return results on demand. To do that, you will use Spring Batch's
org.springframework.batch.item.database.JdbcCursorItemReader . This ItemReader opens a cursor (by
creating a ResultSet) and have a row mapped to a domain object each time the read method is called by
Spring Batch. To configure the JdbcCursorItemReader, you provide a minimum of three dependencies: a
datasource, the query you want to run, and your RowMapper implementation. Listing 7-38 shows the
configuration for your customerItemReader.
Listing 7-38. JDBC Cursor-Based customerItemReader
<beans:bean id="customerItemReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="select * from customer"/>
<beans:property name="rowMapper" ref="customerRowMapper"/>
</beans:bean>
<beans:bean id="customerRowMapper"
class="com.apress.springbatch.chapter7.CustomerRowMapper"/>
 
Search WWH ::




Custom Search