MyBatis3與Spring3無縫集成-從iBatis平滑過渡


從2010開始接觸iBatis到現在,一直到現在把iBatis作為數據訪問層ORM。為了演示一個Web應用,今天又搭了個SpringMVC應用,由於應用比較簡單,Spring版本直接用最新版本3.2.4.RELEASE,結果驚訝的發現,Spring已經不推薦使用iBatis了,SqlMapClientDaoSupport類已經加上了@deprecated as of Spring 3.2, in favor of the native Spring support in the Mybatis follow-up project (http://code.google.com/p/mybatis/)注解。於是乎,簡單的學習了下MyBatis3,在這里把學習筆記記錄下來,希望能對和我一樣,在從iBatis轉入到MyBatis的網友一些參考。

1、增加Maven依賴
由於要整合Spring和MyBatis,所以至少需要spring/springmvc和mybatis-spring的Maven依賴,和Spring一樣,直接使用最新版本:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>

 

2、增加MyBatis的配置文件
和iBatis的SqlMapConfig配置一樣,MyBatis也有一個總配置文件,本應用中為mybatis-config.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>
<typeAliases>
<typeAlias alias="UserDO" type="com.github.obullxl.jeesite.dal.dto.UserDO" />
</typeAliases>

<mappers>
<mapper resource="mybatis/mappers/atom-user-mapper.xml" />
</mappers>
</configuration>

 

在該文件中,增加一個類型別名,類型別名的目的是避免多次輸入過長的類路徑;同時有一個映射文件,它和iBatis的sqlmapping文件一樣,里面是動態SQL內容;熟悉iBatis的朋友會發現,其實MyBatis和iBatis的思路其實差不多的。

3、增加映射文件
從中配置文件和結合iBatis的SqlMap文件可以得出,SQL也是需要映射文件,本應用為atom-user-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="ATOM-USER">
<resultMap id="UserMap" type="UserDO">
<id property="id" column="id" />
<result property="uname" column="uname" />
<result property="passwd" column="passwd" />
<result property="uemail" column="uemail" />
<result property="gmt_create" column="gmtCreate" />
<result property="gmt_modify" column="gmtModify" />
</resultMap>

<select id="findAll" resultMap="UserMap" fetchSize="1">
SELECT * FROM atom_user
</select>

<select id="count" resultType="int">
SELECT COUNT(*) FROM atom_user;
</select>
</mapper>

 

大家發現,和sqlmapping文件非常的相似,其中ResultMap的class用type代替,由於我們的中配置文件中,已經定義了UserDO的別名,所以這里直接用UserDO就行了,無需包含包名稱。

4、增加Spring配置文件
在Spring配置文件中,定義MyBatis相關的Bean,本應用為mybatis-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/osnode?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull" />
<property name="username" value="osnode" />
<property name="password" value="site" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="defaultAutoCommit" value="true" />
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>

<bean id="abstractDAO" abstract="true">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="userDAO" parent="abstractDAO" class="com.github.obullxl.jeesite.dal.mybatis.MyBatisUserDAO" />

<bean id="userService" class="com.github.obullxl.jeesite.service.impl.UserServiceImpl" />
</beans>

 

5、DAO類
接口定義如下:

public interface UserDAO {
public int count();
public List<UserDO> findAll();
}

 

默認實現如下:

public class MyBatisUserDAO extends SqlSessionDaoSupport implements UserDAO {

/** 
* @see com.github.obullxl.jeesite.dal.dao.UserDAO#count()
*/
public int count() {
return this.getSqlSession().selectOne("ATOM-USER.count");
}

/** 
* @see com.github.obullxl.jeesite.dal.dao.UserDAO#findAll()
*/
public List<UserDO> findAll() {
return this.getSqlSession().selectList("ATOM-USER.findAll");
}

}

 

6、測試應用
到目前為止,我們的MyBatis和Spring就整合完成了,寫一個簡單的Java類看看:

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/mybatis-context.xml");
UserService service = context.getBean(UserService.class);

int count = service.count();
logger.warn("Count value is:{}", count);

List<UserDO> users = service.findAll();
logger.warn("All users:\n{}", users);
}

 

哈哈,可以看到我們所希望的結果,有了基本的框架,接下來就是補充應用其他內容了……

7、源代碼
本博客中,代碼只涉及到部分重點代碼,其他代碼我就不在貼出來了,請移步到GitHub:https://github.com/obullxl/atom-jeesite

8、關於本博客
博客園地址:http://www.cnblogs.com/obullxl/p/mybatis-integration-with-spring.html
百度引擎地址:http://obullxl.duapp.com/topic-blog-17.html
紅帽引擎地址:https://obullxl-osnode.rhcloud.com/topic-blog-4.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM