Samuel Learning

  • 联系
  •  
  • 管理

文章分类(203)

  • 01 ASP/HTML(6)
  • 02 Script/CSS(16)
  • 03 C/C++
  • 04 XML(4)
  • 05 微软编程(26)
  • 06 J2EE(60)
  • 09 Linux(14)
  • 10 Database(27)
  • 11 报表打印
  • 12 打包安装(1)
  • 13 模式重构(2)
  • 14 系统架构
  • 15 敏捷开发(11)
  • 16 案例分析(30)
  • 17 Workflow(1)
  • 18 配置管理(1)
  • 19 项目管理
  • 20 英语(4)

新闻分类(52)

  • CXF学习
  • Hibernate学习(1)
  • iBatis学习(16)
  • Struts1学习(1)
  • Struts2学习(34)

.NET资源

  • adxmenu
  • C# Open Source
  • DNN Directory
  • M2Land
  • Windows Form FAQ
  • 中国DNN
  • 中国DNN联盟

Ajax

  • DoJo
  • GWT
  • JQuery
  • jquery autocomplete
  • jquery flexgrid
  • JQuery Form
  • jquery masked input
  • JQuery UI
  • jquery validation
  • Jquery 图表
  • jquery报表
  • jquery插件集合
  • Qooxdoo
  • Tibco GI
  • YUI
  • YUI-EXT
  • ZeroKode

Java开源

  • ABLE
  • Agile Tao
  • Ajax4JSF
  • Alfresco
  • AppFuse
  • Compiere
  • Equinox
  • Findbugs
  • Geronimo
  • Grails
  • Harmony
  • Hibernate论坛
  • JAG
  • Java开源大全
  • Java视线论坛
  • jBPM工作流
  • JSFTutorials
  • Nereide ERP
  • Ofbiz ERP
  • Opentaps ERP
  • operamasks
  • Petstore
  • Prototype.js
  • RIFE
  • Runa
  • SpringSide
  • Struts Menu
  • Sun Glassfish
  • Trails
  • YUI4JSF
  • 满江红

Mobile

  • Sencha

WEB资源

  • DHTML中心
  • DHTML参考手册
  • DHTML文档
  • EclipsePlugin
  • Firebug
  • GRO Clinux
  • jMaki
  • JSTL文档
  • LoadIcon
  • Openlaszlo
  • Struts Menu 展示
  • Web Test Tools
  • WebCtrs
  • Webdeveloper
  • 中国RIA开发者论坛

Workflow

  • E-Workflow
  • JBPM
  • OpenWFE
  • OSWorkflow
  • WFMC
  • Workflow Research

其他连接

  • confach
  • CPP
  • ejay
  • Giovanni
  • 丹佛
  • 交大e-learning
  • 交大研究生院
  • 可恶的猫
  • 天天@blog
  • 我的相册
  • 阿飞

大牛人

  • 32篇JBPM
  • David.Turing
  • HongSoft@业务集成
  • Joel
  • Koen Aers
  • Martinfowler
  • Raible Matt
  • Raible Wiki
  • Scott W.Ambler
  • Tom Baeyens
  • Uncle Bob
  • 一个世界在等待
  • 子在川上曰
  • 小布老师
  • 小明
  • 差沙
  • 徐昊
  • 江南白衣
  • 汪博士
  • 汪小金
  • 银狐999

开源软件

  • 2Bizbox ERP
  • CompiereCRM&ERP
  • EGW
  • Vtiger CRM
  • webERP

敏捷

  • Canoo
  • Cruisecontrol
  • DBUnit
  • EL4Ant
  • Extreme Programming
  • Fit
  • Fitnesse
  • JFrog
  • Liquibase
  • Maven
  • MockObjects
  • Selenium
  • Squish
  • xpairtise
  • XPlanner
  • XProgramming
  • 敏捷联盟

数据库

  • Oracle 中国
  • Oracle-ERP
  • Oracle在线社区

未归类

  • Aquarius Orm Studio
  • mambo建站系统
  • Oracle产品下载
  • 远程同步管理工具Capivara

经典框架

  • Apache Shale
  • formdef-struts
  • FreeMarker 主页
  • JBoss Seam
  • JSF 中心
  • JSF 入门应用
  • JSF中国
  • MyFaces官方
  • Spring 社区
  • Spring专业网站
  • Spring中文论坛
  • Spring参考手册
  • Spring官方网站
  • strecks-struts
  • Struts1
  • Struts2
  • Struts-layout
  • StrutsWiKi
  • Tapestry WIKI
  • Tapestry 官方
  • Tapestry4开发指南
  • Tapestry中文文档
  • Webwork2文档
  • Wicket

网络教程

  • Laliluna
  • RoseIndia
  • Sang Shin
  • Visualbuilder

著名站点

  • Buildix
  • Dev2Dev
  • IBM dev中国
  • InfoQ
  • ITPub
  • Java Eye
  • Java Research
  • JavaRead
  • JavaWorldTW
  • Matrix
  • PHP100
  • PHPX
  • SpringSideWiKi
  • TheServerSide
  • TWPHP
  • 中国工作流论坛

项目管理

  • 管理人网

最新评论

View Post

Converting 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.

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的支持类

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.

下一步,我们删除旧的构造器(有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。

OrderService.java
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类

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拉服务。

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
第一步,建立一个listener让SpringInit在站点启动时初始化

/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里查找-这个时为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.
从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.
这个个工作还在进行中,所以请随意添加备注和建议,使得他更完善。

posted on 2008-07-20 18:25 MingIsMe 阅读(238) 评论(0)  编辑  收藏 所属分类: iBatis学习

 
Powered by:
BlogJava
Copyright © MingIsMe