国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

淺析mybatis和spring整合的實現過程

2019-11-26 14:53:51
字體:
來源:轉載
供稿:網友

根據官方的說法,在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" class="org.mybatis.spring.SqlSessionFactoryBean">   <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" class="org.apache.commons.dbcp.BasicDataSource"   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" class="org.mybatis.spring.SqlSessionFactoryBean">   <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" class="org.mybatis.spring.mapper.MapperFactoryBean"> <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 * from 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; publicinterface BlogMapper {  public Blog selectBlog(int id);  publicvoid insertBlog(Blog blog);  publicvoid updateBlog(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.tiantian.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) {   returnblogMapper.selectBlog(id);  }  public List<Blog> find() {   returnblogMapper.selectAll();  }  publicvoid insertBlog(Blog blog) {   blogMapper.insertBlog(blog);  }  publicvoid updateBlog(Blog blog) {   blogMapper.updateBlog(blog);  }  public BlogMapper getBlogMapper() {   returnblogMapper;  }  @Resource  publicvoid setBlogMapper(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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <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" class="org.apache.commons.dbcp.BasicDataSource"   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" class="org.mybatis.spring.SqlSessionFactoryBean">   <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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">   <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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="basePackage" value="com.tiantian.mybatis.mapper" />  <property name="markerInterface" value="com.tiantian.mybatis.mapper.SuperMapper"/> </bean> 

      如果是都使用了注解MybatisMapper標記的話,那么我們就可以這樣來定義我們的MapperScannerConfigurer。

Xml代碼

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <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" class="org.apache.commons.dbcp.BasicDataSource"   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" class="org.mybatis.spring.SqlSessionFactoryBean">   <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 class="org.mybatis.spring.mapper.MapperScannerConfigurer">   <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" class="org.mybatis.spring.SqlSessionTemplate">  <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; import com.tiantian.mybatis.model.Blog; @Repository publicclass BlogDaoImpl implements BlogDao {  private SqlSessionTemplate sqlSessionTemplate;  publicvoid deleteBlog(int id) {   sqlSessionTemplate.delete("com.tiantian.mybatis.mapper.BlogMapper.deleteBlog", id);  }  public Blog find(int id) {   returnsqlSessionTemplate.selectOne("com.tiantian.mybatis.mapper.BlogMapper.selectBlog", id);  }  public List<Blog> find() {   returnthis.sqlSessionTemplate.selectList("com.tiantian.mybatis.mapper.BlogMapper.selectAll");  }  publicvoid insertBlog(Blog blog) {   this.sqlSessionTemplate.insert("com.tiantian.mybatis.mapper.BlogMapper.insertBlog", blog);  }  publicvoid updateBlog(Blog blog) {   this.sqlSessionTemplate.update("com.tiantian.mybatis.mapper.BlogMapper.updateBlog", 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的整合方法

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莆田市| 准格尔旗| 镇平县| 河西区| 西盟| 中西区| 娱乐| 丹巴县| 察哈| 繁昌县| 兴业县| 沙雅县| 屏边| 迁安市| 从江县| 宁乡县| 鸡泽县| 浦县| 台前县| 晋城| 丹江口市| 贺兰县| 铁力市| 汕尾市| 科技| 唐海县| 洛川县| 苗栗县| 芦山县| 铜梁县| 治县。| 大化| 图木舒克市| 石屏县| 防城港市| 永和县| 襄城县| 商丘市| 广德县| 枣庄市| 大丰市|