zoukankan      html  css  js  c++  java
  • Mybatis的cache

    相关类:org.apache.ibatis.executor.CachingExecutor

    相关代码:

      public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
        BoundSql boundSql = ms.getBoundSql(parameterObject);
        CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
        return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
      }

    从CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);可以看出,当下列四个元素相同时会使用cache:

    MappedStatement(一般为xml中定义)、parameterObject(传递给xml的参数)、rowBounds(分页参数)、boundSql(最终sql,由MappedStatement和parameterObject决定)

    createCacheKey的代码:

      public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
        if (closed) throw new ExecutorException("Executor was closed.");
        CacheKey cacheKey = new CacheKey();
        cacheKey.update(ms.getId());
        cacheKey.update(rowBounds.getOffset());
        cacheKey.update(rowBounds.getLimit());
        cacheKey.update(boundSql.getSql());
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        if (parameterMappings.size() > 0 && parameterObject != null) {
          TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
          if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
            cacheKey.update(parameterObject);
          } else {
            MetaObject metaObject = configuration.newMetaObject(parameterObject);
            for (ParameterMapping parameterMapping : parameterMappings) {
              String propertyName = parameterMapping.getProperty();
              if (metaObject.hasGetter(propertyName)) {
                cacheKey.update(metaObject.getValue(propertyName));
              } else if (boundSql.hasAdditionalParameter(propertyName)) {
                cacheKey.update(boundSql.getAdditionalParameter(propertyName));
              }
            }
          }
        }
        return cacheKey;
      }
  • 相关阅读:
    HTML简介(一)
    Bootstrap简介--目前最受欢迎的前端框架(一)
    命名空间--名称解析规则
    SpringMVC概述(2)
    MVC模型概述(1)
    Luogu P2831 【NOIP2016】愤怒的小鸟|DP
    【学习笔记】凸包
    【学习笔记】Floyd的妙用
    Luogu P2886 [USACO07NOV]牛继电器Cow Relays|最短路,倍增
    Luogu P5463 小鱼比可爱(加强版)|树状数组
  • 原文地址:https://www.cnblogs.com/chanedi/p/4009059.html
Copyright © 2011-2022 走看看