Java Reference
In-Depth Information
Listing 7-12. The FieldSetMapper Interface
package org.springframework.batch.item.file.mapping;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public interface FieldSetMapper<T> {
T mapFieldSet(FieldSet fieldSet) throws BindException;
To create your own mapper, you will implement the FieldSetMapper interface with the type defined
as Customer. From there, as shown in Listing 7-13, you can map each field from the FieldSet to the
domain object, concatenating the addressNumber and street fields into a single address field per your
Listing 7-13. Mapping Fields from the FieldSet to the Customer Object
package com.apress.springbatch.chapter7;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class CustomerFieldSetMapper implements FieldSetMapper<Customer> {
public Customer mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
customer.setAddress(fieldSet.readString("addressNumber") +
" " + fieldSet.readString("street"));
return customer;
The FieldSet methods are very similar to the ResultSet methods of the JDBC realm. Spring provides a
method for each of the primitive data types, String (trimmed or untrimmed), BigDecimal , and
java.util.Date . Each of these different methods has two different varieties. The first takes an integer as
the parameter where the integer represents the index of the field to be retrieved in the record. The other
version, shown in Listing 7-14, takes the name of the field. Although this approach requires you to name
the fields in the job configuration, it's a more maintainable model in the long run. Listing 7-14 shows the
FieldSet interface.
Search WWH ::

Custom Search