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.
iBatis DAO已經(jīng)不建議使用,推薦使用Springframework。這份文檔描述如何轉(zhuǎn)換JPetstore5的iBatis DAO到Spring DAO。
Converting jpetshop5 to ibatis-spring
JPetshop5到IBatis-Spring的轉(zhuǎn)換
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).
前提是有一個(gè)類似jpetstore5的項(xiàng)目,把他鏈接到正常的數(shù)據(jù)庫上,在daoconfig類里支持hssqldb,我們將放棄對(duì)這個(gè)類的支持。
Updating the jar files
更新jar文件
Remove the old iBATIS jar files from the class path and create the reference to the new iBATIS and Spring jars:
刪除舊的ibatis JAR文件,添加到新的ibatis和spring 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
Making the code changes
改變代碼
First, we need to extend the Spring support classes:
首先,我們需要擴(kuò)展spring的支持類
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.
下一步,我們刪除舊的構(gòu)造器(有DaoManager參數(shù)),在所有的sqlMapDao文件里刪除對(duì)DaoManager的引入。
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.
我們還需要從com.ibatis.jpetstore.service包的服務(wù)類里刪除默認(rèn)的構(gòu)造器,因?yàn)镈AO將被Spring注入。
Dealing with transactions (todo)
處理事務(wù)
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里,有一些與事務(wù)有關(guān)的代碼,這個(gè)OrderService我們需要不同的處理,但是目前,我們刪除他,并且刪除從構(gòu)造器里刪除DaoManager。
public OrderService(ItemDao itemDao, OrderDao orderDao, SequenceDao sequenceDao) {
this.itemDao = itemDao;
this.orderDao = orderDao;
this.sequenceDao = sequenceDao;
}
Linking Spring and iBATIS
鏈接Spring和iBatis
We'll add the SpringInit.java class:
我們?cè)黾覵pringInit.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.
當(dāng)網(wǎng)站啟動(dòng)時(shí),這個(gè)類被實(shí)例化,并前包含了一個(gè)對(duì)spring管理的對(duì)象的引用。
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.
展示層必須與spring管理的服務(wù)層的關(guān)聯(lián),所以必須刪除默認(rèn)的構(gòu)造器,因?yàn)槲覀儚膕pring拉服務(wù)。
public AccountBean() {
this(new AccountService(), new CatalogService());
}
Would be replaced with:
上面代碼用下面的代碼替換:
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
第一步,建立一個(gè)listener讓SpringInit在站點(diǎn)啟動(dòng)時(shí)初始化
<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:
這個(gè)告訴你的上下文去/web-inf/spring.xml里查找-這個(gè)時(shí)為jpetstore5創(chuàng)建的文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-
"http:>
<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.
從com.ibatis.jpetstore.persistence包刪除DaoConfig.java類和dao.xml文件。
Conclusion
總結(jié)
This is still a work in progress, so please feel free to add comments and suggestions for making it better.
這個(gè)個(gè)工作還在進(jìn)行中,所以請(qǐng)隨意添加備注和建議,使得他更完善。