Mapper(DAO層)介面如何執行個體化

來源:互聯網
上載者:User

標籤:context   registry   gis   介面   rar   瞭解   直接   spring   aos   

上篇mybatis 對應檔載入是分析了一下我們所有的xml對應檔如何載入的,但在我學習ssm的過程中,發現dao層的介面在service層會注入一個執行個體化對象,直接可以使用,但我們並沒有做dao層的實作類別,

很好奇的去查了查資料,下面就分析一下

mybatis設定檔

 

mybatis是通過org.mybatis.spring.mapper.MapperScannerConfigurer類來實現dao層掃描的 我們進入MapperScannerConfigurer這個類,有一個postProcessBeanDefinitionRegistry方法

  public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {    if (this.processPropertyPlaceHolders) {      processPropertyPlaceHolders();    }    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);    scanner.setAddToConfig(this.addToConfig);    scanner.setAnnotationClass(this.annotationClass);    scanner.setMarkerInterface(this.markerInterface);    scanner.setSqlSessionFactory(this.sqlSessionFactory);    scanner.setSqlSessionTemplate(this.sqlSessionTemplate);    scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);    scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);    scanner.setResourceLoader(this.applicationContext);    scanner.setBeanNameGenerator(this.nameGenerator);    scanner.registerFilters();        //掃描basepackage包下的所有dao介面scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));  }

進入doscan方法

//由父類去找到合格interface類,並轉化為bean類Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);if (beanDefinitions.isEmpty()) {  logger.warn("No MyBatis mapper was found in ‘" + Arrays.toString(basePackages) + "‘ package. Please check your configuration.");} else {//處理找到的interface bean類  processBeanDefinitions(beanDefinitions);}return beanDefinitions;

然後看processBeanDefinitions方法

private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {    GenericBeanDefinition definition;    for (BeanDefinitionHolder holder : beanDefinitions) {      definition = (GenericBeanDefinition) holder.getBeanDefinition();      if (logger.isDebugEnabled()) {        logger.debug("Creating MapperFactoryBean with name ‘" + holder.getBeanName()           + "‘ and ‘" + definition.getBeanClassName() + "‘ mapperInterface");      }      // the mapper interface is the original class of the bean      // but, the actual class of the bean is MapperFactoryBean      definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59

//設定beanclass類型為mapperFactoryBean definition.setBeanClass(this.mapperFactoryBean.getClass()); definition.getPropertyValues().add("addToConfig", this.addToConfig); boolean explicitFactoryUsed = false; if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) { definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionFactory != null) { definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory); explicitFactoryUsed = true; } if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) { if (explicitFactoryUsed) { logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName)); explicitFactoryUsed = true; } else if (this.sqlSessionTemplate != null) { if (explicitFactoryUsed) { logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored."); } definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate); explicitFactoryUsed = true; } if (!explicitFactoryUsed) { if (logger.isDebugEnabled()) { logger.debug("Enabling autowire by type for MapperFactoryBean with name ‘" + holder.getBeanName() + "‘."); } definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); } } }

 

最終會把dao層介面封裝成MapperFactoryBean

也就是說我們會把所有的dao層檔案,封裝成和以下設定檔一樣的效果,是不是很熟悉了

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">        <property name="mapperInterface" value="com.zsh.dao.UserMapper"></property>        <property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>

以上是我們掃描dao層的原理,下面我們繼續分析如何自動執行個體化介面

再說自動執行個體化介面之前我們瞭解一下什麼是動態代理 Java 動態代理機制分析及擴充 

上面是通過Factory 方法建立Bean 
首先我們定位到類 org.mybatis.spring.mapper.MapperFactoryBean

public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements        FactoryBean<T> {    private Class<T> mapperInterface;    private boolean addToConfig;

可以看到該類實現了org.springframework.beans.factory.FactoryBean介面,通過調用 
org.mybatis.spring.mapper.MapperFactoryBean.getObject()方法來獲得Bean

    public T getObject() throws Exception {        return getSqlSession().getMapper(this.mapperInterface);    }

我們通過分析以上的getSqlSession()可以看出getMapper的實作類別是SqlSessionTemplate,我們跳轉到SqlSessionTemplate看一下getMapper方法

    public <T> T getMapper(Class<T> type) {        return getConfiguration().getMapper(type, this);    }

可以看到,它首先調用了自身的getConfiguration()方法返回一個Configuration對象,然後再調用Configuration對象的getMapper方法

我們直接定位到Configuration的getMapper方法

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {    return mapperRegistry.getMapper(type, sqlSession);  }

直接定位到mapperRegistry.getMapper

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);    if (mapperProxyFactory == null) {      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");    }    try {      return mapperProxyFactory.newInstance(sqlSession);    } catch (Exception e) {      throw new BindingException("Error getting mapper instance. Cause: " + e, e);    }  }

在這個方法裡,首先獲得一個MapperProxyFactory的對象mapperProxyFactory,然後調用該對象的newInstance方法建立我們需要的bean,定位到newInstance方法

  public T newInstance(SqlSession sqlSession) {    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);    return newInstance(mapperProxy);  }

最後我們進入newInstance這個重載函數

protected T newInstance(MapperProxy<T> mapperProxy) {    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);  }

在這裡用jdk的動態代理建立了一個代理對象,也就是說,我們最終是對mapper的所有介面分別建立了各自的代理對象,通過代理對象來執行我們的sql

參考並感謝

https://www.cnblogs.com/question-sky/p/6654101.html

http://blog.csdn.net/tanqidong1992/article/details/48026491

https://www.jianshu.com/p/3e619786dd18

Mapper(DAO層)介面如何執行個體化

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.