作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!
- 一、所需类库 | Required jar list
- 二、配置web.xml | How to configure web.xml
- 三、配置log4j | How to configure log4j
- 四、配置C3P0数据库连接池 | How to configure c3p0 jdbc connection pool
- 五、配置Spring与IBatis集成 | How to configure spring integrates with ibatis
- 七、编写DAO类 | Write dao class
- 八、编写JUnit单元测试 | Write junit testcase class
- 附录I:项目结构及完整配置 | Appendix I: Project structure and complete configuration
- 附录II:Spring配置项说明 | Appendix II: Spring configuration items description
已经近三年没有使用Spring,IBatis,Hibernate等框架,同部门的另一个项目组赶进度,我们整个项目组都加入开发的行列。Spring,IBatis如何配置,如何使用等都忘光了,上网找一找资料,看看以前写的项目,花了近半天,完成了 Spring + IBatis + Struts2 的集成。好脑袋不如写博客,记录下来,备用。
集成Spring IBatis 的整个过程如下:
引入所有需要的*.jar文件。
- 配置web.xml。
- 配置log4j。
- 配置C3P0数据库连接池。
- 配置Spring以及与IBatis集成。
- 建立数据库表结构及其domain类和配置
- 编写dao类。
- 编写单元测试类。
一、所需类库 | Required jar list
c3p0-0.9.1.2.jar // 连接池实现类库
commons-logging-1.0.4.jar
ibatis-2.3.4.726.jar
log4j-1.2.15.jar
ojdbc14-10g.jar // Oracle JDBC驱动类库
spring-2.5.6.jar
spring-test.jar // 单元测试需要用到的类库
junit-4.4.jar // 单元测试需要用到的类库
二、配置web.xml | How to configure web.xml
1、在web.xml中增加如下内容:
|
|
三、配置log4j | How to configure log4j
1、在src/main/resouces 目录下建立 log4j.xml,其内容如下:
|
|
四、配置C3P0数据库连接池 | How to configure c3p0 jdbc connection pool
1、在src/main/resouces 目录下建立 jdbc.properties,其内容如下:
|
|
2、然后在Spring的配置ApplicationContext.xml中添加如下配置:
|
|
五、配置Spring与IBatis集成 | How to configure spring integrates with ibatis
1、在src/main/resouces 目录下建立IBatis的SQL映射配置文件SqlMapConfig.xml,其内容如下:
|
|
2、在Spring的配置ApplicationContext.xml中添加如下配置:
|
|
六、建立数据表结构及Domain类 | Create table and its corresponding domain class
1、测试Spring与IBatis集成的表结构如下:
|
|
2、建立User表对应的domain类:User.java,其内容如下:
|
|
3、在与User.java的同一个package下建立User表的SqlMap映射配置文件:USER_SqlMap.xml。其内容如下:
|
|
4、在IBatis的配置文件SqlMapConfig.xml中加入USER_SqlMap.xml:
|
|
七、编写DAO类 | Write dao class
1、编写DAO接口类:UserDAO.java,其内容如下:
|
|
2、编写DAO实现类:UserDAOImpl.java,其内容如下:
|
|
八、编写JUnit单元测试 | Write junit testcase class
1、编写JUnit公用类:DaoBaseTestCase.java,其内容如下:
|
|
2、编写UserDAOImpl的单元测试类:UserDaoImplTest,其内容如下:
|
|
至此,完成Spring与IBatis的集成。
附录I:项目结构及完整配置 | Appendix I: Project structure and complete configuration
2、Spring 配置文件ApplicationContext.xml的完整内容:
|
|
3、IBatis 配置文件SqlMapConfig.xml的完整内容:
|
|
附录II:Spring配置项说明 | Appendix II: Spring configuration items description
1、<context:component-scan/>说明。
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
2、<context:annotationconfig/>说明。
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
3、<tx:annotation-driven /> 说明。
<tx:annotation-driven /> 表示使用声明式事务。如果用 ‘transactionManager’ 来定义 PlatformTransactionManager bean的名字的话,你就可以忽略 <tx:annotation-driven/> 标签里的 ‘transaction-manager’ 属性。 如果 PlatformTransactionManager bean你要通过其它名称来注入的话,你必须用 ‘transaction-manager’ 属性来指定它。