Spring Mybatis初始化

配置

欲在Spring中使用Mybatis,需要引入


  org.mybatis
  mybatis-spring

在Spring配置文件中添加Spring配置

    
    
        
                
        
        
        
                            
                classpath:mapper/*Mapper.xml
            
        
    
    
        
    

dataSource:数据源
configLocation:mybatis的配置文件路径
mapperLocations:mybatis的mapper的文件路径。

初始化过程

Spring-mybatis的初始化入口为

  //org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet()
    public void afterPropertiesSet() throws Exception {
        notNull(dataSource, "Property 'dataSource' is required");
        notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");

        this.sqlSessionFactory = buildSqlSessionFactory();
    }

buildSqlSessionFactory函数可简化为

    protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
        Configuration configuration;
        XMLConfigBuilder xmlConfigBuilder = null;
        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
        configuration = xmlConfigBuilder.getConfiguration();
        //部分代码略
        xmlConfigBuilder.parse();
        //部分代码略
        if (!isEmpty(this.mapperLocations)) {
          for (Resource mapperLocation : this.mapperLocations) {
                if (mapperLocation == null) {
                  continue;
                }
                    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                      configuration, mapperLocation.toString(), configuration.getSqlFragments());
                    xmlMapperBuilder.parse();
            }
        }
        //部分代码略
        return this.sqlSessionFactoryBuilder.build(configuration);
    }

XMLConfigBuilder解析mybaitis的配置文件

    public Configuration parse() {
        if (parsed) {
          throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        }
        parsed = true;
        parseConfiguration(parser.evalNode("/configuration"));
        return configuration;
    }

    private void parseConfiguration(XNode root) {
        try {
          propertiesElement(root.evalNode("properties")); //issue #117 read properties first
          typeAliasesElement(root.evalNode("typeAliases"));
          pluginElement(root.evalNode("plugins"));
          objectFactoryElement(root.evalNode("objectFactory"));
          objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
          settingsElement(root.evalNode("settings"));
          environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631
          databaseIdProviderElement(root.evalNode("databaseIdProvider"));
          typeHandlerElement(root.evalNode("typeHandlers"));
          mapperElement(root.evalNode("mappers"));
        } catch (Exception e) {
          throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
        }
    }

XMLMapperBuilder对Mybatis的mapper文件进行解析

      public void parse() {
        if (!configuration.isResourceLoaded(resource)) {
          configurationElement(parser.evalNode("/mapper"));
          configuration.addLoadedResource(resource);
          bindMapperForNamespace();
        }

        parsePendingResultMaps();
        parsePendingChacheRefs();
        parsePendingStatements();
      }

      public XNode getSqlFragment(String refid) {
        return sqlFragments.get(refid);
      }

      private void configurationElement(XNode context) {
        try {
          String namespace = context.getStringAttribute("namespace");
          if (namespace.equals("")) {
              throw new BuilderException("Mapper's namespace cannot be empty");
          }
          builderAssistant.setCurrentNamespace(namespace);
          cacheRefElement(context.evalNode("cache-ref"));
          cacheElement(context.evalNode("cache"));
          parameterMapElement(context.evalNodes("/mapper/parameterMap"));
          resultMapElements(context.evalNodes("/mapper/resultMap"));
          sqlElement(context.evalNodes("/mapper/sql"));
          buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
        } catch (Exception e) {
          throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
        }
      }

至此,mybatis的配置文件,mapper文件解析完毕

最后通过sqlSessionFactoryBuilder.build函数返回一个SqlSessionFactory对象

    public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
    }

扫描Mapper接口

org.mybatis.spring.mapper.MapperScannerConfigurer

实现了Spring的BeanDefinitionRegistryPostProcessor接口,目的是为了向Spring注入mapper的代理类。

    //org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        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();
        scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
    }

postProcessBeanDefinitionRegistry函数初始化ClassPathMapperScanner并扫描mapper接口所在的包。

    public int scan(String... basePackages) {
        int beanCountAtScanStart = this.registry.getBeanDefinitionCount();

        doScan(basePackages);

        // Register annotation config processors, if necessary.
        if (this.includeAnnotationConfig) {
            AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
        }

        return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
    }

scan调用doScan函数

    public Set doScan(String... basePackages) {
        //获取包下的所有bean的beanDefinitionHolder,并将它们注入到Spring中。
        Set beanDefinitions = super.doScan(basePackages);

        if (beanDefinitions.isEmpty()) {
          logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
        } else {
          for (BeanDefinitionHolder holder : beanDefinitions) {
            GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
            //略
            //修改Bean的class
            definition.setBeanClass(MapperFactoryBean.class);
            //代码略,如果显示的设置了sqlSessionFactory或者sqlSessionTemplate,就使用显示设置的信息,
            //否则将根据类型自动获取Spring中的sqlSessionFactory或者sqlSessionTemplate
            //(definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE))。
            //注:即使Scan未配置sqlSessionFactory,Spring也会自己自动获取容器中的sqlSessionFactory
          }
        }
        return beanDefinitions;
    }

MapperFactoryBean是一个factoryBean.将会在Bean初始化的时候返回Mybatis的mapper的代理类。

MapperFactoryBean

    public class MapperFactoryBean extends SqlSessionDaoSupport implements FactoryBean {
      //部分代码略
      public T getObject() throws Exception {
        return getSqlSession().getMapper(this.mapperInterface);
      }

      public Class getObjectType() {
        return this.mapperInterface;
      }

      public boolean isSingleton() {
        return true;
      }

    }

MapperFactoryBean实现FactoryBean接口,Spring在使用MapperFactoryBean创建对象时会调用getObject()函数。

因为SqlSessionFactory为DefaultSqlSessionFactory,DefaultSqlSessionFactory.openSession()返回SqlSession为DefaultSqlSession,所以上面的getSqlSession().getMapper(this.mapperInterface)最终会调用到DefaultSqlSession.getMapper(this.mapperInterface)函数。

    //org.apache.ibatis.session.defaults.DefaultSqlSession
    public  T getMapper(Class type) {
        return configuration.getMapper(type, this);
    }

configuration.getMapper会调用到MapperRegistry.getMapper函数

    public  T getMapper(Class type, SqlSession sqlSession) {
        final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) 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);
        }
    }

此后将使用动态代理生成mapperinterface的代理对象返回。

你可能感兴趣的:(Spring Mybatis初始化)