Java Reference
In-Depth Information
I should point out that while the rest of the configurations for the job do not need to be changed (the
same ItemWriter will work fine), you will need to update the reference to the customerFileReader in the
copyFileStep to reference your new customerItemReader instead.
For this example, you will ignore the Transaction data that you have been working with in previous
examples. Because of that, you will need to update your Customer's toString to print out valid output.
Instead of printing the number of transactions each customer had, you will print out a formatted
address for each customer. Listing 7-39 shows the updated toString method you can use.
Listing 7-39. Customer.toString
public String toString() {
StringBuilder output = new StringBuilder();
output.append(firstName + " " +
middleInitial + ". " +
lastName + "\n");
output.append(address + "\n");
output.append(city + ", " + state + "\n");
return output.toString();
With the configuration you have now, each time Spring Batch calls the read() method on the
JdbcCursorItemReader, the database will return a single row to be mapped to your domain object and
To run your job, you use the same command you have been using: Java -jar copyJob.jar
jobs/copyJob.xml copyJob outputFile=/output/jdbcOutput.txt . This command will execute your job
generating the same type of output you have in your previous examples.
Although this example is nice, it lacks one key ingredient. The SQL is hardcoded. I can think of very
few instances where SQL requires no parameters. Using the JdbcCursorItemReader, you use the same
functionality to set parameters in your SQL as you would using the JdbcTemplate and a
PreparedStatement. To do this, you need to write an
org.springframework.jdbc.core.PreparedStatementSetter implementation. A PreparedStatementSetter
is similar to a RowMapper; however, instead of mapping a ResultSet row to a domain object, you are
mapping parameters to your SQL statement. If you wanted to get all of the customers in a given city,
your configuration would look like Listing 7-40.
Listing 7-40. Processing Only Customers by a Given City
<beans:bean id="customerItemReader"
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="select * from customer where city = ?"/>
<beans:property name="rowMapper" ref="customerRowMapper"/>
<beans:property name="preparedStatementSetter" ref="citySetter"/>
Search WWH ::

Custom Search