Java Reference
In-Depth Information
to the properties of an object. As a result, DAO s implemented using i BATIS often
contain very little code.
Using a Spring SqlMapClientTemplate
Let's imagine, for example, that you had to write a DAO method that finds the res-
taurants that serve a particular delivery address and time. It takes the delivery
address and time as parameters and returns a list of RestaurantDTO objects. The
DAO would execute this SELECT statement:
SELECT r.*
FROM RESTAURANT r,
RESTAURANT_ZIPCODE rz,
RESTAURANT_TIME_RANGE tr
WHERE rz.ZIPCODE = ?
AND rz.RESTAURANT_ID = r.RESTAURANT_ID
AND tr.RESTAURANT_ID = r.RESTAURANT_ID
AND tr.DAY_OF_WEEK = ?
If you were using JDBC , then you would have to write the usual boilerplate code to
create, initialize, and execute a PreparedStatement and iterate through the
ResultSet creating the DTO s. You would also have to make sure that the Pre-
paredStatement and ResultSet were closed by using a try/finally . In compari-
son, the i BATIS /Spring version of the DAO method is remarkably simple. It
creates a map containing the parameters for the query and executes the query by
calling SqlMapClientTemplate.queryForList() :
public class RestaurantDAOIBatisImpl extends SqlMapClientDaoSupport
implements RestaurantDAO {
public RestaurantDAOIBatisImpl(
SqlMapClientTemplate template) {
setSqlMapClientTemplate(template);
}
Saves SqlMapClientTemplate
public List findAvailableRestaurants(Address deliveryAddress,
Date deliveryTime) {
Calendar c = Calendar.getInstance();
c.setTime(deliveryTime);
int dayOfWeek =
c.get(Calendar.DAY_OF_WEEK);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
String zipCode =
deliveryAddress.getZip();
Creates a Map
containing query
parameters
Map deliveryInfo = new HashMap();
deliveryInfo.put("zipCode", zipCode);
 
Search WWH ::




Custom Search