4.<cache>标签解析剖析

1.首先进入sqlSessionFactoryBuilder.build()方法

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    
    
        SqlSessionFactory var5;
        try {
    
    
        // 核心配置Configuration的XML解析
            XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
            var5 = this.build(parser.parse());
        } catch (Exception var14) {
    
    
            throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
        } finally {
    
    
            ErrorContext.instance().reset();

            try {
    
    
                inputStream.close();
            } catch (IOException var13) {
    
    
            }

        }

2.进入XMLConfigBuilder的parse()方法,再进入parseConfiguration(XNode root)方法,这里利用构建者模式的依次解析核心XML中的各个标签构建Configuration对象

private void parseConfiguration(XNode root) {
    
    
        try {
    
    
            this.propertiesElement(root.evalNode("properties"));
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            this.typeAliasesElement(root.evalNode("typeAliases"));
            this.pluginElement(root.evalNode("plugins"));
            this.objectFactoryElement(root.evalNode("objectFactory"));
            this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
            this.settingsElement(settings);
            this.environmentsElement(root.evalNode("environments"));
            this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            this.typeHandlerElement(root.evalNode("typeHandlers"));
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
    
    
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

3.进入mappers解析方法并找到cache标签的解析方法进入即XMLMapperBuilder的cacheElement(XNode context)方法,这里获取cache标签的各个配置属性,并调用builderAssistant.useNewCache()进行构建。

private void cacheElement(XNode context) throws Exception {
    
    
        if (context != null) {
    
    
            String type = context.getStringAttribute("type", "PERPETUAL");
            Class<? extends Cache> typeClass = this.typeAliasRegistry.resolveAlias(type);
            String eviction = context.getStringAttribute("eviction", "LRU");
            Class<? extends Cache> evictionClass = this.typeAliasRegistry.resolveAlias(eviction);
            Long flushInterval = context.getLongAttribute("flushInterval");
            Integer size = context.getIntAttribute("size");
            boolean readWrite = !context.getBooleanAttribute("readOnly", false);
            boolean blocking = context.getBooleanAttribute("blocking", false);
            Properties props = context.getChildrenAsProperties();
            this.builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
        }

4.进入useNewCache()方法发现,创建一个Cache接口的实现类即二级缓存对象,并分别存入核心配置类Configuration对对象,和MapperBuilderAssistant对象中,由于MapperBuilderAssistant属于XMLMapperBuilder,即XMLMapperBuilder中存在一个二级缓存(又源码得知一个mapper标签一个XMLMapperBuilder对象)。

public Cache useNewCache(Class<? extends Cache> typeClass, Class<? extends Cache> evictionClass, Long flushInterval, Integer size, boolean readWrite, boolean blocking, Properties props) {
    
    
        Cache cache = (new CacheBuilder(this.currentNamespace)).implementation((Class)this.valueOrDefault(typeClass, PerpetualCache.class)).addDecorator((Class)this.valueOrDefault(evictionClass, LruCache.class)).clearInterval(flushInterval).size(size).readWrite(readWrite).blocking(blocking).properties(props).build();
        this.configuration.addCache(cache);
        this.currentCache = cache;
        return cache;
    }

5.继续回到XMLConfigBuilder解析mapperXML的mapperElement(XNode parent)方法,然后进入解析mapperXML的具体方法即XMLMapperBuilder的parse()方法。

XMLConfigBuilder.class

private void mapperElement(XNode parent) throws Exception {
    
    
        if (parent != null) {
    
    
            Iterator var2 = parent.getChildren().iterator();

            while(true) {
    
    
                while(var2.hasNext()) {
    
    
                    XNode child = (XNode)var2.next();
                    String resource;
                    if ("package".equals(child.getName())) {
    
    
                        resource = child.getStringAttribute("name");
                        this.configuration.addMappers(resource);
                    } else {
    
    
                        resource = child.getStringAttribute("resource");
                        String url = child.getStringAttribute("url");
                        String mapperClass = child.getStringAttribute("class");
                        XMLMapperBuilder mapperParser;
                        InputStream inputStream;
                        if (resource != null && url == null && mapperClass == null) {
    
    
                            ErrorContext.instance().resource(resource);
                            inputStream = Resources.getResourceAsStream(resource);
                            mapperParser = new XMLMapperBuilder(inputStream, this.configuration, resource, this.configuration.getSqlFragments());
                            //解析mapperXML的具体方法
                            mapperParser.parse();
                        } else if (resource == null && url != null && mapperClass == null) {
    
    
                            ErrorContext.instance().resource(url);
                            inputStream = Resources.getUrlAsStream(url);
                            mapperParser = new XMLMapperBuilder(inputStream, this.configuration, url, this.configuration.getSqlFragments());
                            mapperParser.parse();
                        } else {
    
    
                            if (resource != null || url != null || mapperClass == null) {
    
    
                                throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
                            }

                            Class<?> mapperInterface = Resources.classForName(mapperClass);
                            this.configuration.addMapper(mapperInterface);
                        }
                    }
                }

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

        this.parsePendingResultMaps();
        this.parsePendingCacheRefs();
        this.parsePendingStatements();
    }

6.找到解析select,insert,update,delete标签的方法,即组装MappedStatement的方法MapperBuilderAssistant的addMappedStatement()方法

public MappedStatement addMappedStatement( String id, ...) {
    
     if (unresolvedCacheRef) {
    
     throw new IncompleteElementException("Cache-ref not yet resolved"); }id = applyCurrentNamespace(id, false); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; 
//创建MappedStatement对象
 MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, id, sqlSource, sqlCommandType) .flushCacheRequired(valueOrDefault(flushCache, !isSelect)) .useCache(valueOrDefault(useCache, isSelect)) 
 // 在这里将之前放到MapperBuilderAssistant中的二级缓存Cache封装到MappedStatement
 .cache(currentCache);
 

总结:
同一个mapperXML中各个增删改查标签公用一个二级缓存,所以说二级缓存是Mapper级别的。
陶某*____*

猜你喜欢

转载自blog.csdn.net/yangxiaofei_java/article/details/111143449