开发环境:
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下载:
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"?> <bean <tx:annotation-driven transaction-manager="transactionManager" /> </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>
|
account-mapper.xml
<?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();
|
6、创建service:
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 |
这里添加@Repository标记,相当于在spring.xml中定义了一个bean,bean的id为这个类对象名称的第一个字母改成小写后的字符串,即:accountDao。添加 @Resource标记,相当于在accountDao这个bean中,引用了一个“sqlSessionFactory”。
3、在AccountService类中添加两个标记:@Service 和 @Resource:
@Service |
4、运行TestAccountService,同样测试通过。