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已经不建议使用,推荐使用Springframework。这份文档描述如何转换JPetstore5的iBatis DAO到Spring DAO。
Converting jpetshop5 to ibatis-spring
JPetshop5到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).
前提是有一个类似jpetstore5的项目,把他链接到正常的数据库上,在daoconfig类里支持hssqldb,我们将放弃对这个类的支持。
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:
首先,我们需要扩展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.
下一步,我们删除旧的构造器(有DaoManager参数),在所有的sqlMapDao文件里删除对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包的服务类里删除默认的构造器,因为DAO将被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里,有一些与事务有关的代码,这个OrderService我们需要不同的处理,但是目前,我们删除他,并且删除从构造器里删除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:
我们增加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.
当网站启动时,这个类被实例化,并前包含了一个对spring管理的对象的引用。
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管理的服务层的关联,所以必须删除默认的构造器,因为我们从spring拉服务。
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
第一步,建立一个listener让SpringInit在站点启动时初始化
<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里查找-这个时为jpetstore5创建的文件。
<?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
总结
This is still a work in progress, so please feel free to add comments and suggestions for making it better.
这个个工作还在进行中,所以请随意添加备注和建议,使得他更完善。