JEQUEL - A Domain Specific Language for the Structured Query Language (SQL) embedded in Java                       SpringExtensions
Jequel /

Extensions to the Spring JDBC-API

BeanRowMapper, BeanRowHandler, ValueRowMapper, ValueRowHandler

Using the RowHandler and RowMapper Interface is great but it doesn't help separating the concerns of database integrated DAO code and the business related code that processes the results of the executed queries.

One still has the dependency on java.sql.ResultSet and on the literal column names of the select statement. (Use Constants or Enums for these to keep them consistent).

Wouldn't it be nice to have a facility using JavaBeans or even real method parameters as transportation means for the data originating from the ResultSet.

E.g. writing something like this, both driven by SQL using an JdbcTemplate.query() or NamedParameterJdbcTemplate.query().

class CalculatePurchasePriceMapper implements BeanRowMapper<PurchasePriceBean,BigDecimal> {
    public BigDecimal mapBean(PurchasePriceBean purchasePriceBean) {
        return calculateFloatingPurchasePrice(purchasePriceBean.getArticleNo(),
                                              purchasePriceBean.getPurchasePriceValue())
               .add(purchasePriceBean.getBasePurchasePrice());
    }
    ...
}
class CustomerDataHandler implements ValueRowHandler {
    public void handleValue(String customerName, Date birthday, boolean update) {
       getCustomerService().getCustomer(customerName).setBirthDay(birthday);
       if (update) getNotificationService().customerUpdated(customerName);
    }
    ...
}

HowTo

The BeanMapper/Handler uses the dynamic Proxy to wrap the ResultSet behind a Java Bean Interface. The method param based approach uses reflection to look up the method parameters and return the correctly converted result set values in the order of the result set (as access to the method parameter names is not possible without external APIs).

Of course these Result Handling approaches are integrated in the Jequel-Execution-API.

References



edit SideBar
Page last modified on 04.11.2007 00:29 Uhr
Bearbeiten - Historie - Druckansicht