欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Spring與Mybatis整合的MapperScannerConfigurer怎么用

這篇文章將為大家詳細(xì)講解有關(guān)Spring與Mybatis整合的MapperScannerConfigurer怎么用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)主營尖山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),尖山h5小程序制作搭建,尖山網(wǎng)站營銷推廣歡迎尖山等地區(qū)企業(yè)咨詢

MapperScannerConfigurer介紹

MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一個(gè)類。

想要了解該類的作用,就得先了解MapperFactoryBean。

MapperFactoryBean的出現(xiàn)為了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate編寫數(shù)據(jù)訪問對象(DAO)的代碼,使用動(dòng)態(tài)代理實(shí)現(xiàn)。

比如下面這個(gè)官方文檔中的配置:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value=http://www.cnblogs.com/fangjian0423/p/"org.mybatis.spring.sample.mapper.UserMapper" />

org.mybatis.spring.sample.mapper.UserMapper是一個(gè)接口,我們創(chuàng)建一個(gè)MapperFactoryBean實(shí)例,然后注入這個(gè)接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean會(huì)使用SqlSessionFactory創(chuàng)建SqlSession)這兩個(gè)屬性。

之后想使用這個(gè)UserMapper接口的話,直接通過spring注入這個(gè)bean,然后就可以直接使用了,spring內(nèi)部會(huì)創(chuàng)建一個(gè)這個(gè)接口的動(dòng)態(tài)代理。

當(dāng)發(fā)現(xiàn)要使用多個(gè)MapperFactoryBean的時(shí)候,一個(gè)一個(gè)定義肯定非常麻煩,于是mybatis-spring提供了MapperScannerConfigurer這個(gè)類,它將會(huì)查找類路徑下的映射器并自動(dòng)將它們創(chuàng)建成MapperFactoryBean。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value=http://www.cnblogs.com/fangjian0423/p/"org.mybatis.spring.sample.mapper" />

這段配置會(huì)掃描org.mybatis.spring.sample.mapper下的所有接口,然后創(chuàng)建各自接口的動(dòng)態(tài)代理類。

MapperScannerConfigurer底層代碼分析

以以下代碼為示例進(jìn)行講解(部分代碼,其他代碼及配置省略):

package org.format.dynamicproxy.mybatis.dao;
public interface UserDao {
    public User getById(int id);
    public int add(User user);    
    public int update(User user);    
    public int delete(User user);    
    public List<User> getAll();    
}

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value=http://www.cnblogs.com/fangjian0423/p/"org.format.dynamicproxy.mybatis.dao"/>

我們先通過測試用例debug查看userDao的實(shí)現(xiàn)類到底是什么。
Spring與Mybatis整合的MapperScannerConfigurer怎么用
我們可以看到,userDao是1個(gè)MapperProxy類的實(shí)例。
看下MapperProxy的源碼,沒錯(cuò),實(shí)現(xiàn)了InvocationHandler,說明使用了jdk自帶的動(dòng)態(tài)代理。

public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialversionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperinterface;
  private final Map<Method, MapperMethod> methodCache;

  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

}

下面開始分析MapperScannerConfigurer的源碼

MapperScannerConfigurer實(shí)現(xiàn)了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一個(gè)可以修改spring工長中已定義的bean的接口,該接口有個(gè)postProcessBeanDefinitionRegistry方法。
Spring與Mybatis整合的MapperScannerConfigurer怎么用

然后我們看下ClassPathMapperScanner中的關(guān)鍵是如何掃描對應(yīng)package下的接口的。
Spring與Mybatis整合的MapperScannerConfigurer怎么用

其實(shí)MapperScannerConfigurer的作用也就是將對應(yīng)的接口的類型改造為MapperFactoryBean,而這個(gè)MapperFactoryBean的屬性mapperInterface是原類型。MapperFactoryBean本文開頭已分析過。

所以最終我們還是要分析MapperFactoryBean的實(shí)現(xiàn)原理!

MapperFactoryBean繼承了SqlSessionDaoSupport類,SqlSessionDaoSupport類繼承DaoSupport抽象類,DaoSupport抽象類實(shí)現(xiàn)了InitializingBean接口,因此實(shí)例個(gè)MapperFactoryBean的時(shí)候,都會(huì)調(diào)用InitializingBean接口的afterPropertiesSet方法。

DaoSupport的afterPropertiesSet方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperFactoryBean重寫了checkDaoConfig方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
然后通過spring工廠拿對應(yīng)的bean的時(shí)候:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
這里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
Configuration的getMapper方法,會(huì)使用MapperRegistry的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperRegistry的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperProxyFactory構(gòu)造MapperProxy:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
沒錯(cuò)! MapperProxyFactory就是使用了jdk組帶的Proxy完成動(dòng)態(tài)代理。
MapperProxy本來一開始已經(jīng)提到。MapperProxy內(nèi)部使用了MapperMethod類完成方法的調(diào)用:
Spring與Mybatis整合的MapperScannerConfigurer怎么用

下面,我們以UserDao的getById方法來debug看看MapperMethod的execute方法是如何走的。

@Test
public void testGet() {
    int id = 1; system.out.println(userDao.getById(id));
}
<select id="getById">

Spring與Mybatis整合的MapperScannerConfigurer怎么用
Spring與Mybatis整合的MapperScannerConfigurer怎么用

關(guān)于“Spring與Mybatis整合的MapperScannerConfigurer怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

文章題目:Spring與Mybatis整合的MapperScannerConfigurer怎么用
分享路徑:http://www.chinadenli.net/article28/pejscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT動(dòng)態(tài)網(wǎng)站定制網(wǎng)站全網(wǎng)營銷推廣微信公眾號移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化