转:spring 3 和mybatis 3集成,并用junit4进行测试
开发环境:
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、
MyBatis3 jar 下载:
http://www.mybatis.org/java.html
junit 4 jar下载:
http://www.junit.org/
2、 添加的jar包如下:
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,目录结构如下:
这是一个标准的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; public class Account implements Serializable { private static final long serialVersionUID = -7970848646314840509L; public Account() { |
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>
|
<?xml version="1.0" encoding="UTF-8"?> <mapper namespace="account"> |
5、创建dao:
package com.glen.dao; import javax.annotation.Resource; import org.apache.ibatis.session.SqlSession; import com.glen.model.Account;
private SqlSessionFactory sessionFactory; public AccountDao() { } public SqlSessionFactory getSessionFactory() { public void setSessionFactory(SqlSessionFactory sessionFactory) { public void insert(Account account) { SqlSession session = sessionFactory.openSession();
|
package com.glen.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.glen.dao.AccountDao;
/** public void setAccountDao(AccountDao accountDao) {
|
Ok,至此spring 和mybatis就整合好了。
三、用junit进行单元测试
在src/test/java目录下,创建一个测试类:TestAccountService:
package com.glen.service; import static org.junit.Assert.assertEquals; import org.apache.log4j.Logger; import com.glen.model.Account;
@Before } // 创建一个帐户 } |
测试通过,显示如下界面:
四、使用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的定义
<!-- |
2、在AccountDao类中添加两个标记:@Repository和 @Resource,
@Repository @Resource |
3、在AccountService类中添加两个标记:@Service 和 @Resource:
@Service |
4、运行TestAccountService,同样测试通过。 下载 jar 包
Spring3 jar 下载: