RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
MyBatis中怎么实现二级缓存

今天就跟大家聊聊有关MyBatis中怎么实现二级缓存,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联公司专业为企业提供宁乡网站建设、宁乡做网站、宁乡网站设计、宁乡网站制作等企业网站建设、网页设计与制作、宁乡企业网站模板建站服务,10年宁乡做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

一、创建Cache的完整过程

我们从SqlSessionFactoryBuilder解析mybatis-config.xml配置文件开始:

Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

然后是:

XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());

看parser.parse()方法:

parseConfiguration(parser.evalNode("/configuration"));

看处理Mapper.xml文件的位置:

mapperElement(root.evalNode("mappers"));

看处理Mapper.xml的XMLMapperBuilder:

XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, 
   resource, configuration.getSqlFragments());
mapperParser.parse();

继续看parse方法:

configurationElement(parser.evalNode("/mapper"));

到这里:

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"));

从这里看到namespace就是xml中元素的属性。然后下面是先后处理的cache-ref和cache,后面的cache会覆盖前面的cache-ref,但是如果一开始cache-ref没有找到引用的cache,他就不会被覆盖,会一直到最后处理完成为止,最后如果存在cache,反而会被cache-ref覆盖。这里是不是看着有点晕、有点乱?所以千万别同时配置这两个,实际上也很少有人会这么做。

看看MyBatis如何处理

private void cacheElement(XNode context) throws Exception {
  if (context != null) {
    String type = context.getStringAttribute("type", "PERPETUAL");
    Class typeClass = typeAliasRegistry.resolveAlias(type);
    String eviction = context.getStringAttribute("eviction", "LRU");
    Class 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);
  }
}

从源码可以看到MyBatis读取了那些属性,而且很容易可以到这些属性的默认值。

创建Java的cache对象方法为builderAssistant.useNewCache,我们看看这段代码:

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

从调用该方法的地方,我们可以看到并没有使用返回值cache,在后面的过程中创建MappedStatement的时候使用了currentCache。

二、使用Cache过程

在系统中,使用Cache的地方在CachingExecutor中:

@Override
public  List query(
    MappedStatement ms, Object parameterObject, 
    RowBounds rowBounds, ResultHandler resultHandler, 
    CacheKey key, BoundSql boundSql) throws SQLException {
 Cache cache = ms.getCache();

获取cache后,先判断是否有二级缓存。

只有通过,或@CacheNamespace,@CacheNamespaceRef标记使用缓存的Mapper.xml或Mapper接口(同一个namespace,不能同时使用)才会有二级缓存。

 if (cache != null) {

如果cache存在,那么会根据sql配置(,

像上面这个查询,你会写到那个xml中呢??

不管是写到RoleMapper.xml还是UserRoleMapper.xml,或者是一个独立的XxxMapper.xml中。如果使用了二级缓存,都会导致上面这个查询结果可能不正确。

如果你正好修改了这个用户的角色,上面这个查询使用缓存的时候结果就是错的。

这点应该很容易理解。

在我看来,就以MyBatis目前的缓存方式来看是无解的。多表操作根本不能缓存。

如果你让他们都使用同一个namespace(通过)来避免脏数据,那就失去了缓存的意义。

看完上述内容,你们对MyBatis中怎么实现二级缓存有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前文章:MyBatis中怎么实现二级缓存
文章路径:http://sczitong.cn/article/jgihsi.html