Mybatis Cache key 源码级

  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());                            //sql语句
    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;


总结: 默认使用 perpetualCache

cache key = 方法名+ sql 语句+参数类型  +参数值+额外参数

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/80767566
今日推荐