Java Reference
In-Depth Information
Because the interface has changed, you will have to change the implementation as
well. To support named parameter bind variables, the implementation class inherits
from another convenient class called NamedParameterJdbcDaoSupport , as in Listing 5-8.
Listing 5-8. PolicyDaoImpl.java
public class PolicyDetailDaoImpl extends NamedParameterJdbcDaoSupport
implements PolicyDetailDao{
public void savePolicyDetails(Map policyDetailMap) {
this.getNamedParameterJdbcTemplate().update(SAVE_POLICY_DETAILS_SQL,
policyDetailMap);
}
}
It is noteworthy that our DAO code has shrunk even further. The Map object is impor-
tant in this case; the keys of the objects stored in the map must match those of the
named bind variables in the SQL. Hence, even if the positions of the parameters or bind
variables change in the SQL query string, the code does not break. This is a useful feature,
and it is quite feasible to pass a Map from the HttpServletRequest in the page controller all
the way down to the DAO. This would save a lot of effort because we no longer need to
develop and maintain form beans.
Spring DAO Callbacks
As we already know, JdbcTemplate implements the template design pattern. So, the
algorithm implemented by this class can be altered at suitable points by supplying cus-
tomized logic. In the examples discussed so far, I have allowed the template class to set
the JDBC bind variables for us. In some scenarios, you may be interested in controlling
the setting of these variables. One such case is when database-specific datatypes such as
Oracle's XMLType are used. Listing 5-9 shows the modified DAO implementation class.
Listing 5-9. PolicyDaoImpl.java
public class PolicyDetailDaoImpl extends JdbcDaoSupport implements PolicyDetailDao{
public void savePolicyDetails(String productCd,String name,int age) {
this.getJdbcTemplate().update(PolicyDetailDao.SAVE_POLICY_DETAILS_SQL,
new SavePolicyPreparedStatementSetter(productCd,name,age));
}
}
 
Search WWH ::




Custom Search