根據官方的說法,在ibatis3,也就是Mybatis3問世之前,Spring3的開發工作就已經完成了,所以Spring3中還是沒有對Mybatis3的支持。因此由Mybatis社群自己開發了一個Mybatis-Spring用來滿足Mybatis用戶整合Spring的需求。以下將透過Mybatis-Spring來整合Mybatis跟Spring的用法做一個簡單的介紹。
MapperFactoryBean
首先,我們需要從Mybatis官網上下載Mybatis-Spring的jar包加入到我們專案的類路徑下,當然也需要加入Mybatis的相關jar包和Spring的相關jar包。我們知道在Mybatis的所有操作都是基於一個SqlSession的,而SqlSession是由SqlSessionFactory來產生的,SqlSessionFactory又是由SqlSessionFactoryBuilder來產生的。但是Mybatis-Spring是基於SqlSessionFactoryBean的。在使用Mybatis-Spring的時候,我們也需要SqlSession,而這個SqlSession是內嵌在程式中的,一般不需要我們直接存取。 SqlSession也是由SqlSessionFactory來產生的,但是Mybatis-Spring為我們封裝了一個SqlSessionFactoryBean,在這個bean裡面還是透過SqlSessionFactoryBuilder來建立對應的SqlSessionFactory,進而獲得對應的SqlSession。透過SqlSessionFactoryBean我們可以透過對其指定一些屬性來提供Mybatis的一些設定資訊。所以接下來我們需要在Spring的applicationContext設定檔中定義一個SqlSessionFactoryBean。
Xml程式碼
<bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/tiantian/ckeditor/mybatis/mappers/*Mapper.xml" / > <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model" /> </bean>
在定義SqlSessionFactoryBean的時候,dataSource屬性是必須指定的,它表示用於連接資料庫的資料來源。當然,我們也可以指定一些其他的屬性,以下簡單列舉幾個:
mapperLocations:它表示我們的Mapper檔案存放的位置,當我們的Mapper檔案跟對應的Mapper介面處於同一位置的時候可以不用指定該屬性的值。
configLocation:用於指定Mybatis的設定檔位置。如果指定了該屬性,那麼會以該設定檔的內容作為設定資訊來建構對應的SqlSessionFactoryBuilder,但後續屬性指定的內容會覆寫該設定檔裡面指定的對應內容。
typeAliasesPackage:它一般對應我們的實體類別所在的包,這個時候會自動取對應包中不包括包名的簡單類別名作為包含包名的別名。多個package之間可以用逗號或分號等來分隔。
typeAliases:陣列類型,用來指定別名的。指定了這個屬性後,Mybatis會把這個類型的短名稱作為這個類型的別名,前提是該類別上沒有標註@Alias註解,否則將使用該註解對應的值作為此種類型的別名。
Xml程式碼
<property name="typeAliases"> <array> <value>com.tiantian.mybatis.model.Blog</value> <value>com.tiantian.mybatis.model.Comment</value> </array> </property >
plugins:陣列類型,用來指定Mybatis的Interceptor。
typeHandlersPackage:用來指定TypeHandler所在的套件,如果指定了該屬性,SqlSessionFactoryBean會自動把該套件下面的類別註冊為對應的TypeHandler。多個package之間可以用逗號或分號等來分隔。
typeHandlers:陣列類型,表示TypeHandler。
接下來就是在Spring的applicationContext檔案中定義我們想要的Mapper物件對應的MapperFactoryBean了。透過MapperFactoryBean可以取得到我們想要的Mapper物件。 MapperFactoryBean實作了Spring的FactoryBean接口,所以MapperFactoryBean是透過FactoryBean介面中定義的getObject方法來取得對應的Mapper物件的。在定義一個MapperFactoryBean的時候有兩個屬性需要我們注入,一個是Mybatis-Spring用來產生實作了SqlSession介面的SqlSessionTemplate物件的sqlSessionFactory;另一個就是我們要回傳的對應的Mapper介面了。
定義好對應Mapper介面對應的MapperFactoryBean之後,我們就可以把我們對應的Mapper介面注入到由Spring管理的bean物件中了,例如Service bean物件。這樣當我們需要使用到對應的Mapper介面時,MapperFactoryBean會從它的getObject方法中取得對應的Mapper接口,而getObject內部還是透過我們注入的屬性呼叫SqlSession介面的getMapper(Mapper介面)方法來傳回對應的Mapper接口的。這樣就透過把SqlSessionFactory和對應的Mapper介面交給Spring管理實現了Mybatis跟Spring的整合。
Spring的applicationContext.xml設定檔:
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: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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring- mvc-3.0.xsd"> <context:component-scan base-package="com.tiantian.mybatis"/> <context:property-placeholder location="classpath:config/jdbc.properties"/> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name ="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name ="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml"/> <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" /> </bean> <bean id="blogMapper"> <property name="mapperInterface" value="com.tiantian.mybatis.mapper.BlogMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> </beans>
BlogMapper.xml檔:
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="com.tiantian.mybatis.mapper.BlogMapper"> <!-- 新增記錄--> <insert id="insertBlog" parameterType="Blog" useGeneratedKeys="true" keyProperty="id"> insert into t_blog(title,content,owner) values(#{title},#{content},#{owner}) </ insert> <!-- 查詢單一記錄--> <select id="selectBlog" parameterType="int" resultMap="BlogResult"> select * 從 t_blog where id = #{id} </select> <!-- 修改記錄--> <update id="updateBlog" parameterType="Blog"> update t_blog set title = #{title},content = # {content},owner = #{owner} where id = #{id} </update> <!-- 查詢所有記錄--> <select id="selectAll" resultType="Blog"> select * from t_blog </select> <!-- 刪除記錄--> <delete id="deleteBlog" parameterType="int"> delete from t_blog where id = #{id } </delete> </mapper>
BlogMapper.java:
Java程式碼
package com.tiantian.mybatis.mapper; import java.util.List; import com.tiantian.mybatis.model.Blog; publicinterinsBlogMapper { public Blog selectBlog(int id); publicvoid faceertBlog(Blog blog); ); publicvoid deleteBlog(int id); public List<Blog> selectAll(); }
BlogServiceImpl.java:
Java程式碼
package com.tiantian.mybatis.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.tiantian.mybatis.mapper.BlogMapper; import com.titim. mybatis.model.Blog; import com.tiantian.mybatis.service.BlogService; @Service publicclass BlogServiceImpl implements BlogService { private BlogMapper blogMapper; publicvoid deleteBlog(int id) { blogMapper.deleteBlog(id); } public Blog find(int id) returnMapper. ; } public List<Blog> find() { returnblogMapper.selectAll(); } publicvoid insertBlog(Blog blog) { blogMapper.insertBlog(blog); } publicvoid updateBlog(Blog blog) { blogMapper.updateBlog(blog); (BlogMapper blogMapper) { this.blogMapper = blogMapper; } }
MapperScannerConfigurer
利用上面的方法進行整合的時候,我們有一個Mapper就需要定義一個對應的MapperFactoryBean,當我們的Mapper比較少的時候,這樣做也還可以,但是當我們的Mapper相當多時我們再這樣定義一個個Mapper對應的MapperFactoryBean就顯得速度比較慢了。為此Mybatis-Spring為我們提供了一個叫做MapperScannerConfigurer的類,透過這個類Mybatis-Spring會自動為我們註冊Mapper對應的MapperFactoryBean物件。
如果我們需要使用MapperScannerConfigurer來幫我們自動掃描和註冊Mapper介面的話我們需要在Spring的applicationContext設定檔中定義一個MapperScannerConfigurer對應的bean。對於MapperScannerConfigurer而言有一個屬性是我們必須指定的,那就是basePackage。 basePackage是用來指定Mapper介面檔案所在的基底包的,在這個基底包或其所有子包下面的Mapper介面都會被搜尋到。多個基底包之間可以使用逗號或分號來分隔。最簡單的MapperScannerConfigurer定義就是只指定一個basePackage屬性,如:
Xml程式碼
<bean> <property name="basePackage" value="com.tiantian.mybatis.mapper" /> </bean>
這樣MapperScannerConfigurer就會掃描指定基底包下面的所有接口,並把它們註冊為一個MapperFactoryBean物件。當使用MapperScannerConfigurer加basePackage屬性的時候,我們上面例子的applicationContext設定檔會變成這樣:
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: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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring- mvc-3.0.xsd"> <context:component-scan base-package="com.tiantian.mybatis" /> <context:property-placeholder location="classpath:config/jdbc.properties" /> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name ="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name ="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml" /> <property name="typeAliasesPackage" value="com.tiantian.mybatis.model" /> </bean> <bean> <property name="basePackage" value="com.tiantian.mybatis.mapper" /> </bean> < /beans>
有時候我們指定的基包下面的並不全是我們定義的Mapper接口,為此MapperScannerConfigurer還為我們提供了另外兩個可以縮小搜索和註冊範圍的屬性。一個是annotationClass,另一個是markerInterface。
annotationClass:當指定了annotationClass的時候,MapperScannerConfigurer將只註冊使用了annotationClass註解標記的介面。
markerInterface:markerInterface是用來指定一個介面的,當指定了markerInterface之後,MapperScannerConfigurer只會註冊繼承自markerInterface的介面。
如果上述兩個屬性都指定了的話,那麼MapperScannerConfigurer將取它們的並集,而不是交集。即使用了annotationClass進行標記或是繼承自markerInterface的介面都會被註冊為一個MapperFactoryBean。
現在假設我們的Mapper介面都繼承了一個SuperMapper接口,那麼我們就可以這樣來定義我們的MapperScannerConfigurer。
Xml程式碼
<bean> <property name="basePackage" value="com.tiantian.mybatis.mapper" /> <property name="markerInterface" value="com.tiantian.mybatis.mapper.SuperMapper"/> </bean>
如果是都使用了註解MybatisMapper標記的話,那麼我們就可以這樣來定義我們的MapperScannerConfigurer。
Xml程式碼
<bean> <property name="basePackage" value="com.tiantian.mybatis.mapper" /> <property name="annotationClass" value="com.tiantian.mybatis.annotation.MybatisMapper"/> </bean>
除了用來縮小註冊Mapper介面範圍的屬性之外,我們還可以指定一些其他屬性,如:
sqlSessionFactory:這個屬性已經廢棄。當我們使用了多個資料來源的時候我們就需要透過sqlSessionFactory來指定在註冊MapperFactoryBean的時候需要使用的SqlSessionFactory,因為在沒有指定sqlSessionFactory的時候,會以Autowired的方式自動注入一個。換言之當我們只使用一個資料來源的時候,也就是只定義了一個SqlSessionFactory的時候我們就可以不給MapperScannerConfigurer指定SqlSessionFactory。
sqlSessionFactoryBeanName:它的功能跟sqlSessionFactory是一樣的,只是它指定的是定義好的SqlSessionFactory對應的bean名稱。
sqlSessionTemplate:這個屬性已經廢棄。它的功能也是相當於sqlSessionFactory的,因為就像前面說的那樣,MapperFactoryBean最終還是使用的SqlSession的getMapper方法取的對應的Mapper物件。當定義有多個SqlSessionTemplate的時候才需要指定它。對於一個MapperFactoryBean來說SqlSessionFactory和SqlSessionTemplate只需要其中一個就可以了,當兩者都指定了的時,SqlSessionFactory會被忽略。
sqlSessionTemplateBeanName:指定需要使用的sqlSessionTemplate對應的bean名稱。
注意:由於使用sqlSessionFactory和sqlSessionTemplate屬性時會使一些內容在PropertyPlaceholderConfigurer之前加載,導致在配置文件中使用到的外部屬性信息無法被及時替換而出錯,因此官方現在新的Mybatis-Spring中已經把sqlSessionFactory和sqlSessionTemplate屬性廢棄了,推薦大家使用sqlSessionFactoryBeanName屬性和sqlSessionTemplateBeanName屬性。
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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mybatis="http://www.mybatis.org/schema/mybatis" xsi:schemaLocation="http://www.springframework .org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.mybatis.org/schema/mybatis http:/ /www.mybatis.org/schema/mybatis/mybatis-spring.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.tiantian.mybatis" /> <context:property-placeholder location= "classpath:config/jdbc.properties" /> <bean id="dataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc. username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/tiantian/mybatis/mapper/*.xml" /> <property name= "typeAliasesPackage" value="com.tiantian.mybatis.model" /> </bean> <bean> <property name="basePackage" value="com.tiantian.mybatis.mapper" /> <property name="markerInterface" value="com.tiantian.mybatis.mapper.SuperMapper"/> <property name="sqlSessionFactoryBeanName" value=" sqlSessionFactory"/> </bean> </beans>
SqlSessionTemplate
除了上述整合之後直接使用Mapper介面之外,Mybatis-Spring也為我們提供了直接使用SqlSession的方式。 Mybatis-Spring為我們提供了一個實作了SqlSession介面的SqlSessionTemplate類,它是線程安全的,可以被多個Dao同時使用。同時它也跟Spring的事務進行了關聯,確保目前被使用的SqlSession是一個已經和Spring的事務進行綁定了的。而且它還可以自己管理Session的提交和關閉。當使用了Spring的事務管理機制後,SqlSession也可以跟著Spring的事務一起提交回滾。
使用SqlSessionTemplate時我們可以在Spring的applicationContext設定檔中如下定義:
<bean id="sqlSession"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
這樣我們就可以透過Spring的依賴注入在Dao中直接使用SqlSessionTemplate來編程了,這時候我們的Dao可能會是這個樣子:
Java程式碼
package com.tiantian.mybatis.dao; import java.util.List; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.stereotype.Repository; Blog; @Repository publicclass BlogDaoImpl implements BlogDao { private SqlSessionTemplate sqlSessionTemplate; publicvoid deleteBlog(int id) { sqlSessionTemplate.delete("com.tiantian.mybatis.mapper.BlogMapper.deleteBlog", id); returnsqlSessionTemplate.selectOne("com.tiantian.mybatis.mapper.BlogMapper.selectBlog", id); } public List<Blog> find() { returnthis.sqlSessionTemplate.selectList("com.tiantian.mybatis.mapper.AllBlogMapperselect.AllBlogMapperselect.AllB. ); } publicvoid insertBlog(Blog blog) { this.sqlSessionTemplate.insert("com.tiantian.mybatis.mapper.BlogMapper.insertBlog", blog); } publicvoid updateBlog(Blog blog) { this.sqlSessionTemplate.update("com.tiantian.mybatis.mup.BlogMapper. , blog); } public SqlSessionTemplate getSqlSessionTemplate() { returnsqlSessionTemplate; } @Resource publicvoid setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } }
註:
本文是基於Mybatis3.2.1、Mybatis-Spring1.1.0和Spring3.1寫的。
以上就是本文跟大家介紹的淺析mybatis和spring整合的實作過程,希望大家喜歡,接下來下篇文章要跟大家介紹spring與mybatis的整合方法。