Dashboard > iBATIS DataMapper > Home > Converting iBATIS DAO to Spring DAO > Information > Page Comparison
Converting iBATIS DAO to Spring DAO
Version 1 by Larry Meadors
on Apr 12, 2007 06:36.


 
compared with
Current by Larry Meadors
on Apr 12, 2007 09:11.

(show comment)
 
Key
These lines were removed. This word was removed.
These lines were added. This word was added.

View page history


There are 38 changes. View first change.

 The iBATIS DAO has been depreciated and it is recommended that you use the Spring framework instead. This document describes the steps taken to convert from iBATIS DAO to Spring DAO.
  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.
  
 h2. 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).
  
 h3. 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 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
  
* 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
  
  
 Com.ibatis.jpetstore.persistence.sqlmapdao
  
 Change the BaseSqlMapDao to;
  
 package com.ibatis.jpetstore.persistence.sqlmapdao;
  
 import org.springframework.orm.ibatis.SqlMapClientTemplate;
  
  h3. Making the code changes
 First, we need to extend the Spring support classes:
 {code:title=BaseSqlMapDao.java}
 public class BaseSqlMapDao extends SqlMapClientTemplate {
 
  protected static final int PAGE_SIZE = 4;
 
 }
{code}
  
All the other SqlMapDao files;
  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.
  
Remove the public AccountSqlMapDao(DaoManager daoManager) methode
  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.
  
Remove import com.ibatis.dao.client.DaoManager;
 Com.ibatis.jpetstore.service
  h3. 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.
  
The DAO will be injected by spring, thus the constructor can be deleted
  
 Example for AccountService, remove the public AccountService()methode
  
 (also remove import com.ibatis.dao.client.DaoManager;)
  
 There is a transaction in OrderService.java, this transaction must be handled by Spring, remove it here for now. Also remove it out of the constructor;
  
  {code:title=OrderService.java}
 public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao sequenceDao) {
 
  this.itemDao = itemDao;
 
  this.orderDao = orderDao;
 
  this.sequenceDao = sequenceDao;
 
  }
  
  }
 {code}
  
The Link between spring and ibatis
  h3. Linking Spring and iBATIS
  
Add the SpringInit.java class to a new package com.listeners
  We'll add the SpringInit.java class:
 {code:title=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;
  }
 }
 {code}
  
This class will be instantiated on load of the website and will pass the reference to the spring managed objects
 Com.ibatis.jpetstore.presentation
  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.
  
The presentation layer must connect with the spring managed service layer
  
 So replace;
  
  {code:title=AccountBean.java}
 public AccountBean() {
 
  this(new AccountService(), new CatalogService());
}
 {code}
  
}
  
 With
  
  Would be replaced with:
 {code:title=AccountBean.java}
 public AccountBean() {
this(
  (AccountService) SpringInit.getApplicationContext().getBean("accountService"),
  (CatalogService) SpringInit.getApplicationContext().getBean("catalogService")
  );
 }
 {code}
  
this((AccountService) SpringInit.getApplicationContext().getBean("accountService"),
  This would be repeated for all of the other classes in the presentation package.
  
(CatalogService) SpringInit.getApplicationContext().getBean("catalogService"));
  h3. Configuration
  
}
  
 And add import com.listeners.SpringInit;
  
 Repeat for the other classes
  
  
 Configuration
  
 First setup the listeners to have SpringInit initiated on loading of the site
  
Add to web.xml (after <description> tag)
  
  {code:title=/WEB-INF/web.xml}
 <context-param>
 
  <param-name>contextConfigLocation</param-name>
  
  <param-value>/WEB-INF/spring.xml</param-value>
  
   <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-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  
 
  
 <listener>
 
  <listener-class>com.listeners.SpringInit</listener-class>
  
   <listener-class>com.listeners.SpringInit</listener-class>
 </listener>
{code}
  
 
  This tells your context to look for /WEB-INF/spring.xml - this is the one created for jpetstore5:
  
As you can see it is looking for /WEB-INF/spring.xml create this file. Attached is the one I created for jpetstore5
  {code:title=/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>
 {code}
  
 
 Cleanup
  h3. Cleanup
  
Throw away DaoConfig.java and dao.xml out of com.ibatis.jpetstore.persistence, and the reference to it in the Service classes
  Remove the DaoConfig.java class and the dao.xml file from the com.ibatis.jpetstore.persistence package.
  
  
  h2. 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