bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Spring與Mybatis整合的MapperScannerConfigurer怎么用

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

成都創新互聯主營尖山網站建設的網絡公司,主營網站建設方案,重慶App定制開發,尖山h5小程序制作搭建,尖山網站營銷推廣歡迎尖山等地區企業咨詢

MapperScannerConfigurer介紹

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

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

MapperFactoryBean的出現為了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate編寫數據訪問對象(DAO)的代碼,使用動態代理實現。

比如下面這個官方文檔中的配置:

<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是一個接口,我們創建一個MapperFactoryBean實例,然后注入這個接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean會使用SqlSessionFactory創建SqlSession)這兩個屬性。

之后想使用這個UserMapper接口的話,直接通過spring注入這個bean,然后就可以直接使用了,spring內部會創建一個這個接口的動態代理。

當發現要使用多個MapperFactoryBean的時候,一個一個定義肯定非常麻煩,于是mybatis-spring提供了MapperScannerConfigurer這個類,它將會查找類路徑下的映射器并自動將它們創建成MapperFactoryBean。

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

這段配置會掃描org.mybatis.spring.sample.mapper下的所有接口,然后創建各自接口的動態代理類。

MapperScannerConfigurer底層代碼分析

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

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的實現類到底是什么。
Spring與Mybatis整合的MapperScannerConfigurer怎么用
我們可以看到,userDao是1個MapperProxy類的實例。
看下MapperProxy的源碼,沒錯,實現了InvocationHandler,說明使用了jdk自帶的動態代理。

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實現了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一個可以修改spring工長中已定義的bean的接口,該接口有個postProcessBeanDefinitionRegistry方法。
Spring與Mybatis整合的MapperScannerConfigurer怎么用

然后我們看下ClassPathMapperScanner中的關鍵是如何掃描對應package下的接口的。
Spring與Mybatis整合的MapperScannerConfigurer怎么用

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

所以最終我們還是要分析MapperFactoryBean的實現原理!

MapperFactoryBean繼承了SqlSessionDaoSupport類,SqlSessionDaoSupport類繼承DaoSupport抽象類,DaoSupport抽象類實現了InitializingBean接口,因此實例個MapperFactoryBean的時候,都會調用InitializingBean接口的afterPropertiesSet方法。

DaoSupport的afterPropertiesSet方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperFactoryBean重寫了checkDaoConfig方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
然后通過spring工廠拿對應的bean的時候:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
這里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
Configuration的getMapper方法,會使用MapperRegistry的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperRegistry的getMapper方法:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
MapperProxyFactory構造MapperProxy:
Spring與Mybatis整合的MapperScannerConfigurer怎么用
沒錯! MapperProxyFactory就是使用了jdk組帶的Proxy完成動態代理。
MapperProxy本來一開始已經提到。MapperProxy內部使用了MapperMethod類完成方法的調用:
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怎么用

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

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

成都網站建設公司_創新互聯,為您提供ChatGPT動態網站定制網站全網營銷推廣微信公眾號移動網站建設

廣告

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

搜索引擎優化