Dashboard > iBATIS DataMapper > Home > Converting iBATIS DAO to Spring DAO
Converting iBATIS DAO to Spring DAO
Added by Larry Meadors, last edited by Larry Meadors on Apr 12, 2007  (view change)
Labels: 
(None)


The iBATIS DAO has been depreciated and it is recommended that you use the Spring framework instead. This document (originally posted to the user-java list by a user) describes the steps taken to convert jpetstore5 from iBATIS DAO to Spring DAO.

Converting jpetshop5 to ibatis-spring

Prerequisite is to have a project similar to jpetstore5, and have it connected to a 'normal' database

There is support for hssqldb in the DaoConfig class what we are going to give up (for now).

Updating the jar files

Remove the old iBATIS jar files from the class path and create the reference to the new iBATIS and Spring jars:

  • Remove ibatis-common-2.jar
  • Remove ibatis-dao-2.jar
  • Remove ibatis-sqlmap-2.jar
  • Add ibatis-2.3.0.677.jar
  • Add spring.jar
  • Add commons-dbcp-1.2.1.jar
  • Add commons-pool-1.2.jar
  • Add commons-collections.jar

Making the code changes

First, we need to extend the Spring support classes:

BaseSqlMapDao.java
public class BaseSqlMapDao extends SqlMapClientTemplate {
    protected static final int PAGE_SIZE = 4;
    }

Next, we can remove the old constructors (that have the DaoManager parameter), as well as the import for the DaoManager from all of the other SqlMapDao files.

We also need to remove the default constructors from the service classes in the com.ibatis.jpetstore.service package, because the DAO will be injected by spring.

Dealing with transactions (todo)

There is some code to work with transactions in OrderService.java which we'll need to handle differently, but for the moment, we'll remove it here, and remove the DaoManager from the constructor.

OrderService.java
public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao sequenceDao) {
    this.itemDao = itemDao;
    this.orderDao = orderDao;
    this.sequenceDao = sequenceDao;
    }

Linking Spring and iBATIS

We'll add the SpringInit.java class:

SpringInit.java
public class SpringInit implements ServletContextListener {
    private static WebApplicationContext springContext;
    
    public void contextInitialized(ServletContextEvent event) {
    springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }
    
    public void contextDestroyed(ServletContextEvent event) {
    springContext = null;
    }
    
    public static ApplicationContext getApplicationContext() {
    return springContext;
    }
    }

This class will be instantiated when the website starts, and will contain a reference to the spring managed objects.

The presentation layer must connect with the spring managed service layer, so remove the default constructors because we'll be pulling the services from Spring instead.

AccountBean.java
public AccountBean() {
    this(new AccountService(), new CatalogService());
    }

Would be replaced with:

AccountBean.java
public AccountBean() {
    this(
    (AccountService) SpringInit.getApplicationContext().getBean("accountService"),
    (CatalogService) SpringInit.getApplicationContext().getBean("catalogService")
    );
    }

This would be repeated for all of the other classes in the presentation package.

Configuration

First setup the listeners to have SpringInit initiated on loading of the site

/WEB-INF/web.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring.xml</param-value>
    </context-param>
    
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <listener>
    <listener-class>com.listeners.SpringInit</listener-class>
    </listener>

This tells your context to look for /WEB-INF/spring.xml - this is the one created for jpetstore5:

/WEB-INF/spring.xml
<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
            "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:properties/database.properties"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
    </bean>
    <bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
    <value>classpath:com/ibatis/jpetstore/persistence/sqlmapdao/sql/sql-map-config.xml</value>
    </property>
    <property name="useTransactionAwareDataSource">
    <value>true</value>
    </property>
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>
    <bean id="sqlMapClientTemplate"
    class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="accountDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="categoryDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.CategorySqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="itemDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.ItemSqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="orderDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.OrderSqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="productDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.ProductSqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="sequenceDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.SequenceSqlMapDao">
    <property name="sqlMapClient">
    <ref bean="sqlMapClient"/>
    </property>
    </bean>
    <bean id="accountService" class="com.ibatis.jpetstore.service.AccountService">
    <constructor-arg index="0" ref="accountDao"/>
    </bean>
    <bean id="catalogService" class="com.ibatis.jpetstore.service.CatalogService">
    <constructor-arg index="0" ref="categoryDao"/>
    <constructor-arg index="1" ref="itemDao"/>
    <constructor-arg index="2" ref="productDao"/>
    </bean>
    <bean id="orderService" class="com.ibatis.jpetstore.service.OrderService">
    <constructor-arg index="0" ref="itemDao"/>
    <constructor-arg index="1" ref="orderDao"/>
    <constructor-arg index="2" ref="sequenceDao"/>
    </bean>
    </beans>

Cleanup

Remove the DaoConfig.java class and the dao.xml file from the com.ibatis.jpetstore.persistence package.

Conclusion

This is still a work in progress, so please feel free to add comments and suggestions for making it better.


Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators