Java Reference
In-Depth Information
public Object mapRow(ResultSet rs, int arg1)
throws SQLException {
Customer customer = new Customer();
customer.setId(rs.getLong("id"));
customer.setFirstName(rs.getString("firstName"));
customer.setLastName(rs.getString("lastName"));
customer.setTaxId(rs.getString("ssn"));
customer.setAddress(buildAddress(rs));
return customer;
}
private Address buildAddress(ResultSet rs)
throws SQLException {
Address address = new Address();
address.setAddress1(rs.getString("address1"));
address.setCity(rs.getString("city"));
address.setState(rs.getString("state"));
address.setZip(rs.getString("zip"));
return address;
}
});
if (customers != null && customers.size() > 0) {
return customers.get(0);
} else {
return null;
}
}
}
CustomerDaoJdbc
in Listing 10-14 is definitely more robust than it needs to be. As you can see, the
findCustomerByTaxId
method maps a full
Customer
object when you really only need its id. However, this
approach makes the DAO much more reusable in the long run.
Next on the list of DAOs is
AccountDaoJdbc
. This is the same situation as the
Customer
DAO you just
looked at. This DAO provides the ability to look up an
Account
by account number and returns a full
Account
object including transactions. Listing 10-15 shows the code.
Listing 10-15.
AccountDaoJdbc
package com.apress.springbatch.statement.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;