当前位置:编程学习 > 网站相关 >>

spring 3和mybatis 3集成,并用junit4进行测试

最近一个项目使用的是struts2+Spring3+mybatis3的技术框架,由于开发人员都不熟悉如何进行单元测试,今天有空,简单研究了一下如何用junit4来测试基于这个框架的代码。由于struts的action只是负责前台的请求转发,而所有的业务都是在service层处理,因此一般情况下只需对service进行单元测试,而不需要对action进行单元测试。下面介绍一个简单的例子:
开发环境:

System:Windows xp
IDE:eclipse Java EE 3.6
Database:MySQL

开发依赖库:

JavaEE5、Spring 3.0.5、Mybatis 3.0.4、myBatis-spring-1.0、junit4.8.1

一、准备工作:


1、下载jar包
Spring3 jar下载:

http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE

MyBatis3 jar 下载:
http://www.mybatis.org/java.html
junit 4 jar下载:
http://www.junit.org/


 

2、 添加的jar包如下:

\
3、创建mysql的数据库表,步骤如下:

 1、进入mysql的安装路径,假设在:C:Program FilesMySQLMySQL Server 5.1in;
2、输入命令:mysql -uroot -p,enter,输入密码:admin;
3、mysql>use test;
5、mysql>grant all privileges on test.* to test@localhost identified by test;
6、mysql>flush 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 "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

   

</configuration>
 
上面的配置文件中,可以加入一些公共、常用的MyBatis方面的全局配置。如handler、objectFactory、plugin、以及mappers的映射路径(由于在spring配置文件spring.xml中的SqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等。这个文件名称和下面的spring.xml中的configLocation中的值对应,不是随便写的。
3、创建spring的配置文件spring.xml <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/jee
   http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 <bean
  class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
 
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
  <property name="username" value="test" />
  <property name="password" value="test" />
 </bean>
 
 <!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了 -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>

 <tx:annotation-driven transaction-manager="transactionManager" />
 <!-- myBatis文件 -->

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="classpath:mybatis.xml" />
  <property name="mapperLocations"
   value="classpath*:com/glen/model/*.xml" />
  <property name="dataSource" ref="dataSource" />
 </bean>
 
 <bean id="accountDao" class="com.glen.dao.AccountDao">
  <property name="sessionFactory" ref="sqlSessionFactory"/>
 </bean>
 
 <bean id="accountService" class="com.glen.service.AccountService">
  <property name="accountDao" ref="accountDao"/>
 </bean>
 
 <context:annotation-config />
 <context:component-scan base-package="com.glen" />

</beans>

 
4、JavaBean(Model、Entity)相关类、及mybatis 的mapper对象

javabean: package com.glen.model;

import java.io.Serializable;
import java.util.Date;

public class Account implements Serializable {

 private static final long serialVersionUID = -7970848646314840509L;
 
 private Integer accountId;
 private String username;
 private String password;
 private Date createTime;

 public Account() {
  super();
 }
//下面是getter、setters
 


account-resultMap.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="accountMap

补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,