詳情可見官方文檔http://www.mybatis.org/spring/zh/index.html
一、需要配置的對象實例
1.SqlSessionFactoryBean
在 MyBatis-Spring配置中,SqlSessionFactoryBean 是用於創建 SqlSessionFactory 的。
要配置這個工廠 bean,放置下面的代碼在 Spring 的 XML 配置文件中:
<!-- 指定數據源和配置文件路徑 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/mapping/*.xml"></property> </bean>
要注意 SqlSessionFactory 需要一個 DataSource
2.DataSourceTransactionManager 事務管理器
要 開 啟 Spring 的 事 務 處 理 , 在 Spring 的 XML 配 置 文 件 中 簡 單 創 建 一 個 DataSourceTransactionManager 對象:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
3.MapperFactoryBean
通過MapperFactoryBean指定對應的Dao層接口(也就是Mapper接口),還有sqlSessionFactory
MapperFactoryBean 創建的代理類實現了Mapper 接口,並且注入到應用程序中。
如果 Mapper 有一個對應的 MyBatis 的 XML 映射器文件, 如果 XML 文件在類路徑的 位置和映射器類相同時, 它會被 MapperFactoryBean 自動解析。
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
4.MapperScannerConfigurer
指定Dao層所在的包,以及sqlSessionFactoryBeanName
沒有必要在 Spring 的 XML 配置文件中注冊所有的映射器。
相反,你可以使用一個 MapperScannerConfigurer , 它將會查找類路徑下所有的 映 射 器 並 自 動 將 它 們 創 建 成 MapperFactoryBean。
注 意 , 沒 有 必 要 去 指 定 SqlSessionFactory 或 SqlSessionTemplate , 因 為 MapperScannerConfigurer 將會創建 MapperFactoryBean,之后自動裝配。
但是,如果你使 用了一個以上的 DataSource ,那 么自動裝配可能會失效 。這種 情況下 ,你可以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 屬性來設置正確的 bean 名 稱來使用。
<!-- DAO接口所在包名,Spring會自動查找其下的類,並將其定義為一個Spring Bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
二、Mybatis中的重要對象
1.SqlSession
SqlSession是用於執行持久化操作的對象,類似於JDBC中的Connection。
它提供了面向數據庫執行SQL命令所需的所有方法,可以通過SqlSession實例直接運行已映射的SQL語句。
SqlSession對應着一次數據庫回話。
由於數據庫會話不是永久的,因此SqlSession的生命周期也不應該是永久的。相反,在每次訪問數據庫時都需要創建它。
需要注意的是,每個線程都有自己的SqlSession實例,SqlSession實例不能被共享,也不是線程安全的。
因此最佳的作用域范圍是request作用域或者方法體作用域內。
2.SqlSessionFactory。
每個數據庫對應一個 SqlSessionFactory。
所以,如果你想連接兩個數據庫,你需要創建兩個 SqlSessionFactory 實例。以此類推。
3,關於SqlSession,SqlSessionFactory,SqlSessionFactoryBean的關系。
Mybatis核心是獲取 SqlSession 實例。
要獲得 SqlSession 實例,則需要依賴 SqlSessionFactory 實例,通過openSession()方法獲得。
而 SqlSessionFactory 是 SqlSessionFactoryBuilder 依據 MyBatis 配置文件中的數據源、Sql映射文件等信息來構建的。
在 MyBatis 中,SqlSessionFactory 的實例需要使用 SqlSessionFactoryBuilder 創建;
而在集成環境中,則可以使用 MyBatis-Spring 整合包中的 SqlSessionFactoryBean 來代替。
SqlSessionFactoryBean 封裝了使用 SqlSessionFactoryBuilder 創建 SqlSessionFactory 的過程,
我們可以在 Spring 中以配置文件的形式,通過配置 SqlSessionFactoryBean 獲得 SqlSessionFactory 實例。
源碼細節,類似如下所示:
public class MybatisTest { @Test public void findUserByIdTest(){ InputStream inputStream=null; SqlSession sqlSession=null; try { inputStream=Resources.getResourceAsStream("SqlMapConfig.xml"); //1.創建會話工廠,傳入mybatis的配置文件信息 SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream); //2.通過工廠得到SqlSession sqlSession=factory.openSession(); //3.通過SqlSession操作數據庫 User user=sqlSession.selectOne("test.findUserById", 1); System.out.println(user.toString()); } catch (IOException e) { e.printStackTrace(); }finally{ if(sqlSession!=null){ sqlSession.close(); } if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
三、示例如下:
spring-mybatis.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" > <list> <value>classpath:jdbc.properties </value> <value>classpath:generator.properties </value> </list> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- 指定數據源和配置文件路徑 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/mapping/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類,並將其定義為一個Spring Bean --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務管理)transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>