spring 3和mybatis 3集成,并用junit4进行测试.doc
文本预览下载声明
最近一个项目使用的是struts2+Spring3+mybatis3的技术框架,由于开发人员都不熟悉如何进行单元测试,今天有空,简单研究了一下如何用junit4来测试基于这个框架的代码。由于struts的action只是负责前台的请求转发,而所有的业务都是在service层处理,因此一般情况下只需对service进行单元测试,而不需要对action进行单元测试。下面介绍一个简单的例子:
System:Windows xpIDE:eclipse Java EE 3.6Database:MySQL
开发依赖库:
JavaEE5、Spring 3.0.5、Mybatis 3.0.4、myBatis-spring-1.0、junit4.8.1一、准备工作:
1、jar包Spring3 jar下载:
/repository/app/library/version/detail?name=org.springframework.springversion=3.0.5.RELEASE
MyBatis3 jar 下载:/java.htmljunit 4 jar下载:/
?
2、 jar包如下:
3、创建mysql的数据库表,步骤如下:
1、进入mysql的安装路径,假设在:C:\Program Files\MySQL\MySQL Server 5.1\bin;2、输入命令:mysql -uroot -p,enter,输入密码:admin;3、mysqluse test;5、mysqlgrant all privileges on test.* to?test@localhost identified by test;6、mysqlflush privileges;4、mysql? create table account_bak(account_id int not null auto_increment,???????????????????? username varchar(20),???????????????????? password varchar(20),???????????????????? create_time datetime,???????????????????? primary key(account_id)); 二、spring 和mybatis整合1、在eclipse中创建一个java project,目录结构如下:这是一个标准的maven工程的目录结构,下面逐一介绍上图涉及到的文件。2、创建mybatis的配置文件mybatis.xml
?xml version=1.0 encoding=UTF-8 ?
!DOCTYPE configuration PUBLIC -////DTD Config 3.0//EN /dtd/mybatis-3-config.dtd
configuration
???
/configuration 上面的配置文件中,可以加入一些公共、常用的MyBatis方面的全局配置。如handler、objectFactory、plugin、以及mappers的映射路径(由于在spring配置文件spring.xmlSqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等。这个文件名称和下面的spring.xml中的configLocation中的值对应,不是随便写的。3、创建spring的配置文件spring.xml
?xml version=1.0 encoding=UTF-8?beans xmlns=/schema/beans?xmlns:xsi=/2001/XMLSchema-instance xmlns:p=/schema/p?xmlns:context=/schema/context?xmlns:jee=/schema/jee xmlns:tx=/schema/tx?xsi:schemaLocation=???/schema/beans ???/schema/beans/spring-beans-2.5.xsd???/schema/context ???/schema/context/spring-context-2.5.xsd???/schema/jee ???/schema/jee/spring-jee-2.5.xsd???/schema/tx ???/schema/tx/spring-tx-2.5.xsd
?bean??class=org.springframework.aop.framework.autoproxy.DefaultAdv
显示全部