2.1 解析cache标签

在上一篇中我们找到了Mapper.xml文件的入口,在入口中主要是解析各种标签,这节我们看看mybatis如何解析cache标签。

cache-ref

解析cache-ref主要是在XMLMapperBuilder的cacheRefElement方法中,下面我们来看看这个方法

    private void cacheRefElement(XNode context) {
        if (context != null) {
            //在一步我也不知道做什么用的
            configuration.addCacheRef(builderAssistant.getCurrentNamespace(), context.getStringAttribute("namespace"));


            CacheRefResolver cacheRefResolver = new CacheRefResolver(builderAssistant,
                                                                     context.getStringAttribute("namespace"));
            try {
                cacheRefResolver.resolveCacheRef();
            } catch (IncompleteElementException e) {
                configuration.addIncompleteCacheRef(cacheRefResolver);
            }
        }
    }

cacheRefResolver.resolveCacheRef(); 中什么也没做,直接调用了MapperBuilderAssistant.useCacheRef方法。

下面我们看看useCacheRef方法的内容(仅仅展示关键代码)

public Cache useCacheRef(String namespace) {
		unresolvedCacheRef = true;
		Cache cache = configuration.getCache(namespace);
		if (cache == null) {
		    throw new IncompleteElementException("No cache for namespace '" + namespace + "' could be found.");
		}
		currentCache = cache;
		unresolvedCacheRef = false;
		return cache;
    }

解析cache-ref标签,主要是通过cache-ref标签中namespace属性,获取关于cache的对象。(为什么不把真正的cache保存起来,如果不保存起来如何使用呢?)

cache

下面我们看看cache标签的解析,解析cache主要是在XMLMapperBuilder.cacheElement方法中

    private void cacheElement(XNode context) throws Exception {
		 String type = context.getStringAttribute("type", "PERPETUAL");
		 Class<? extends Cache> typeClass = typeAliasRegistry.resolveAlias(type);
		 String eviction = context.getStringAttribute("eviction", "LRU");
		 Class<? extends Cache> evictionClass = 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();
		 builderAssistant.useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
    }

首先在cache标签中,获取了cache中的各种属性,获取完各种属性后,调用了MapperBuilderAssistant.useCache方法。

下面我们看看MapperBuilderAssistant.useCache方法

   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(currentNamespace).implementation(valueOrDefault(typeClass, PerpetualCache.class))
                                                        .addDecorator(valueOrDefault(evictionClass, LruCache.class))
                                                        .clearInterval(flushInterval)
                                                        .size(size)
                                                        .readWrite(readWrite)
                                                        .blocking(blocking)
                                                        .properties(props)
                                                        .build();
        configuration.addCache(cache);
        currentCache = cache;
        return cache;
    }

前面6个参数,是cache标签中我们关于缓存的一些属性。最后一个参数props是cache标签中,property标签中的属性值的集合。前面6个参数主要是用来初始化Mybatis的cache对象。但是对于我们自定义的缓存对象,Mybatis并不知道应该如何初始化该缓存对象。那么我们可以在property中设置关于我们自己的缓存参数。例如:

public class CustomCache implements Cache {

    private int size;

    private int flush;
}

那我们配置的cache标签如下:

<cache type = 'CustomCache' .... >
	<property name='size' value = '10'></property>
	...
</cache>

这节我们看到了,Mybatis是如何解析cache标签的。Mybatis把cache标签解析映射成了Cache对象。
然后把cache添加到Configuration中的caches中key:mapper.xml的namespace,value:cache对象。

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值