设为首页 收藏本站
查看: 841|回复: 0

[经验分享] spring 3 和mybatis 3集成,并用junit4进行测试

[复制链接]

尚未签到

发表于 2016-11-27 09:22:59 | 显示全部楼层 |阅读模式
  转: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、
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包如下:
DSC0000.jpg
3、创建mysql的数据库表,步骤如下:


1、进入mysql的安装路径,假设在:C:\Program Files\MySQL\MySQL Server 5.1\bin;
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,目录结构如下:
DSC0001.bmp

这是一个标准的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&amp;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">
      <resultMap type="com.hoo.entity.Account" id="accountResultMap">
         <id property="accountId" column="account_id"/>
         <result property="username" column="username"/>
         <result property="password" column="password"/>
         <result property="createTime" column="create_time"/>
      </resultMap>
  </mapper>


account-mapper.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="account">
  
 <select id="getList" parameterType="com.glen.model.Account" resultType="list" resultMap="accountResultMap">
   select * from account where username like '%' #{username} '%'
 </select>
 
 <select id="getAllAccount" resultType="list" resultMap="accountResultMap">
   select * from account
 </select>
 
 
 <!-- accountResultMap是account-resultmap.xml中定义的resultmap -->
 <select id="get" parameterType="com.glen.model.Account" resultType="com.glen.model.Account" resultMap="accountResultMap">
  <![CDATA[
   select * from account where account_id = #{accountId}
        ]]>
 </select>
 
 
 <!-- 自动生成id策略 -->
 <insert id="add" useGeneratedKeys="true" keyProperty="accountId" parameterType="com.glen.model.Account">
  insert into account(account_id, username, password)
  values(#{accountId,jdbcType=BIGINT}, #{username}, #{password})
<!--将最后插入的逐渐返回到java对象-->
  <selectKey resultType="int" keyProperty="accountId">
   SELECT LAST_INSERT_ID()
  </selectKey>
  
 </insert>
 
 <update id="edit" parameterType="com.glen.model.Account">
  update account set
  username = #{username},
  password = #{password}
  where account_id = #{accountId}
 </update>
 
 <delete id="remove" parameterType="com.glen.model.Account">
  delete from account where account_id = #{accountId}
 </delete>
  
</mapper> 



5、创建dao:
  package com.glen.dao;
  import javax.annotation.Resource;
  import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
  import com.glen.model.Account;
  
public class AccountDao {
  private SqlSessionFactory sessionFactory;
  public AccountDao() {
  }
  public SqlSessionFactory getSessionFactory() {
  return sessionFactory;
 }
  public void setSessionFactory(SqlSessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }
  public void insert(Account account) {
  SqlSession session = sessionFactory.openSession();
  session.insert("account.add", account);
 }
 
 public Account getAccountById(Account account) {
  
  SqlSession session = sessionFactory.openSession();
  Account accountFromDb = (Account)session.selectOne("account.get", account);
  return accountFromDb;
 }
}


6、创建service:
  package com.glen.service;
  import javax.annotation.Resource;
  import org.springframework.stereotype.Service;
  import com.glen.dao.AccountDao;
import com.glen.model.Account;
  
public class AccountService {
 
 private AccountDao accountDao;
 
 /**
  * 新增一个帐户。
  * @param account
  */
 public void insertAccount(Account account) {
  
  accountDao.insert(account);
 }
  /**
  * 根据帐户ID查找帐户信息
  * @param account
  * @return
  */
 public Account getAccountById(Account account) {
  
  return accountDao.getAccountById(account);
 }
 
 public AccountDao getAccountDao() {
  return accountDao;
 }
  public void setAccountDao(AccountDao accountDao) {
  this.accountDao = accountDao;
 }
}



Ok,至此spring 和mybatis就整合好了。

三、用junit进行单元测试
在src/test/java目录下,创建一个测试类:TestAccountService:
  package com.glen.service;
  import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
  import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
  import com.glen.model.Account;
  
public class TestAccountService {
 
 Logger logger = Logger.getLogger("TestAccountService");
 
 AccountService service = null;
  @Before
 public void init() {
  
  ApplicationContext aCtx = new FileSystemXmlApplicationContext(
    "classpath:spring.xml");
  AccountService service = (AccountService) aCtx
    .getBean("accountService");
  assertNotNull(service);
  this.service = service;
  }
 
 @Test
 public void testInsertAccount() {
  // 创建一个帐户
  Account account = new Account();
  // account.setAccountId(1);
  account.setUsername("selina");
  account.setPassword("123456");
  
  //将创建的帐户插入到数据库中
  service.insertAccount(account);
  logger.debug("account id: " + account.getAccountId());
  
  //从数据库获取刚才插入的帐户
  Account accountFromDb = service.getAccountById(account);
  assertNotNull(accountFromDb);
  assertEquals(account.getAccountId(), accountFromDb.getAccountId());
 }
  }



测试通过,显示如下界面:
DSC0002.bmp

四、使用spring的标记来注入对象
如上所述,我们在spring的配置文件spring.xml中,定义了两个业务模块相关的bean,accountDao和accountService,但是在实际项目中,这样的dao和service会非常多,如果每个都要这样定义,会造成配置文件的体积过大,可阅读性和可维护性都会变差。

那么如何对spring.xml进行瘦身呢?有两种方案,第一种方案是分模块开发,对于模块内部的bean,写在对应模块内部的spring配置文件中,如:spring-account.xml;第二种方案,就是使用spring的标记。下面我想说说的就是,用spring的标记:@Service @Repository @Resource来实现对象的注入。在上面这个例子基础上,做以下步骤的修改:

1、注释掉spring.xml中的两个bean:accountDao和accountService的定义
  <!-- 
 <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>
 -->



2、在AccountDao类中添加两个标记:@Repository和 @Resource,
  @Repository
public class AccountDao {
   @Resource
 private SqlSessionFactory sessionFactory; 


这里添加@Repository标记,相当于在spring.xml中定义了一个bean,bean的id为这个类对象名称的第一个字母改成小写后的字符串,即:accountDao。添加 @Resource标记,相当于在accountDao这个bean中,引用了一个“sqlSessionFactory”。

3、在AccountService类中添加两个标记:@Service 和 @Resource:
  @Service
public class AccountService {
 
 @Resource
 private AccountDao accountDao;



4、运行TestAccountService,同样测试通过。 下载jar包
Spring3 jar下载:

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306029-1-1.html 上篇帖子: mybatis insert 时 null值的报错问题 下篇帖子: 10/26/2011
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表