
Originally Posted by
yogish
Hi,
Thanks for the reply... it will be great if you can share sample code that covers Abstraction Layer...(generic classes) that handles multiple resultsets with your custom XML schema..
and custom XML schema is specific to your framework..?
Yogish
The class [B]DatabaseAccessGateway[B] is the actual interface that is injected into each DAO. As you can see, it is similar to typical iBatis style interface, bunch of gets, updates/deletes.
Anyway the real implementation is the DatabaseAccessGatewayImpl which uses Spring's BeanWrapper & BeanWrapperImpl classes.
The XML schema looks something like this
Code:
<sql-procedures>
<procedure id="getFacilities" storedProcedure="XXXX.f_get_XXX">
<params>
<param id="facilityResults" direction="OUT" type="CURSOR" />
<param id="ID" direction="IN" type="NUMBER" />
<param id="name" direction="IN" type="VARCHAR" />
<param id="selectionKey" direction="IN" type="VARCHAR" /> -->
</params>
<beans>
<bean type="xxxx.xxxx.domain.Facility" ref-out-param="facilityResults">
<property-mapping>
<property name="facilityID" db-column="XXX_ID" />
<property name="name" db-column="XXXX_NAME" />
<!-- rest omitted -->
</property-mapping>
</bean>
</beans>
</procedure>
I will leave the exercise of parsing this xml into JavaBeans upto you. I used Digester to quickly parse all this.
The main code is basically creation of the StoredProcedure class for the corresponding <procedure tags
The inner class does this trick
Code:
public class CustomDataRowMapper extends AbstractRowBeanMapper {
public CustomDataRowMapper(BeanInfo beanInfo) {
super(beanInfo);
}
public void mapRow(BeanWrapper beanWrapper, Map propertyMappers,
ResultSet rs, int rowNumber) throws SQLException {
Iterator iterator = propertyMappers.entrySet().iterator();
while (iterator.hasNext()) {
Entry entry= (Entry) iterator.next();
PropertyMapper propertyMapper = (PropertyMapper) entry.getValue();
ObjectHandlerUtils.setPropertyValueFromResultSet(beanWrapper,
propertyMapper, rs);
}
}
}
And the AbstractRowBeanMapper, as you might have guessed, is an abstract class that implements RowMapper
Code:
protected abstract void mapRow(BeanWrapper wrapper, Map propertyMappers, ResultSet rs, int rowNumber) throws SQLException;
/**
* Originally called by Spring, it simply creates the new bean, and delegates it to the child class
*/
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
BeanWrapper beanWrapper= ObjectHandlerUtils.getBeanWrapper(beanInfo.getType());
mapRow(beanWrapper, beanInfo.getPropertyMappers(), rs, rowNumber);
return beanWrapper.getWrappedInstance();
}
The key is creation of the BeanWrapper class...
Everything else is just fairly simple. BTW ObjectHandlerUtils is just a simple utils class, it abstracts way all of the pain of handling exceptions.