Search in sources :

Example 1 with MybatisHanlerInitException

use of com.mendmix.mybatis.exception.MybatisHanlerInitException in project jeesuite-libs by vakinge.

the class CacheHandler method generateQueryMethodCacheByMethod.

/**
 * 按查询方法生成缓存key前缀
 * @param entityClassName
 * @param method
 * @return
 */
private QueryCacheMethodMetadata generateQueryMethodCacheByMethod(MapperMetadata mapperMeta, MapperMethod mapperMethod) {
    Method method = mapperMethod.getMethod();
    Cache cacheAnnotation = method.getAnnotation(Cache.class);
    String[] evictOnMethods = cacheAnnotation.evictOnMethods();
    Class<?> mapperClass = mapperMeta.getMapperClass();
    Class<?> entityClass = mapperMeta.getEntityClass();
    QueryCacheMethodMetadata methodCache = new QueryCacheMethodMetadata();
    methodCache.methodName = mapperClass.getName() + InvocationVals.DOT + method.getName();
    methodCache.concurrency = cacheAnnotation.concurrency();
    methodCache.uniqueIndex = cacheAnnotation.uniqueIndex();
    methodCache.cacheGroupKey = entityClass.getSimpleName() + GROUPKEY_SUFFIX;
    if (cacheAnnotation.userScope()) {
        methodCache.contextParam = CURRENT_USER_CONTEXT_NAME;
    } else if (cacheAnnotation.scopeContext().length > 0) {
        methodCache.contextParam = cacheAnnotation.scopeContext()[0];
    }
    if (cacheAnnotation.refKey().length > 0) {
        methodCache.refKey = cacheAnnotation.refKey()[0];
    }
    if (methodCache.contextParam != null && evictOnMethods.length == 0) {
        evictOnMethods = new String[] { "*" };
    }
    methodCache.checkExpired = evictOnMethods.length > 0;
    if (cacheAnnotation.expire() > 0) {
        methodCache.expire = cacheAnnotation.expire();
    } else if (cacheAnnotation.userScope()) {
        methodCache.expire = IN_1MINS * 10 < defaultCacheExpire ? IN_1MINS * 10 : defaultCacheExpire;
    }
    if (methodCache.uniqueIndex && method.getReturnType() != entityClass) {
        throw new MybatisHanlerInitException("@Cache with[uniqueIndex = true] but ReturnType not Match [" + entityClass.getName() + "]");
    }
    methodCache.collectionResult = method.getReturnType() == List.class || method.getReturnType() == Set.class;
    if (methodCache.collectionResult) {
        methodCache.groupRalated = true;
    } else {
        // count等统计查询
        methodCache.groupRalated = method.getReturnType().isAnnotationPresent(Table.class) == false;
    }
    methodCache.fieldNames = new String[method.getParameterTypes().length];
    Annotation[][] annotations = method.getParameterAnnotations();
    boolean uniqueQuery = method.getReturnType().isAnnotationPresent(Table.class);
    for (int i = 0; i < annotations.length; i++) {
        Annotation[] aa = annotations[i];
        if (aa.length > 0) {
            String fieldName = null;
            inner: for (Annotation annotation : aa) {
                if (annotation.toString().contains(Param.class.getName())) {
                    fieldName = ((Param) annotation).value();
                    break inner;
                }
            }
            if (uniqueQuery && mapperMeta.getEntityMetadata().getProp2ColumnMappings().containsKey(fieldName)) {
                methodCache.fieldNames[i] = fieldName;
            }
        }
    // 
    }
    methodCache.keyPattern = new StringBuilder(entityClass.getSimpleName()).append(InvocationVals.DOT).append(method.getName()).append(":%s").toString();
    if (uniqueQuery) {
        for (String name : methodCache.fieldNames) {
            if (StringUtils.isBlank(name)) {
                methodCache.fieldNames = null;
                break;
            }
        }
    }
    // 
    buildEvictOnMethods(mapperClass.getName(), mapperMethod, evictOnMethods);
    return methodCache;
}
Also used : Method(java.lang.reflect.Method) MapperMethod(com.mendmix.mybatis.metadata.MapperMetadata.MapperMethod) Annotation(java.lang.annotation.Annotation) Param(org.apache.ibatis.annotations.Param) MybatisHanlerInitException(com.mendmix.mybatis.exception.MybatisHanlerInitException) Cache(com.mendmix.mybatis.plugin.cache.annotation.Cache)

Example 2 with MybatisHanlerInitException

use of com.mendmix.mybatis.exception.MybatisHanlerInitException in project jeesuite-libs by vakinge.

the class PaginationHandler method start.

@Override
public void start(JeesuiteMybatisInterceptor context) {
    this.dbType = DatabaseType.valueOf(MybatisConfigs.getDbType(context.getGroupName()));
    logger.info("dbType:{}", dbType.name());
    List<MapperMetadata> mappers = MybatisMapperParser.getMapperMetadatas(context.getGroupName());
    for (MapperMetadata e : mappers) {
        Class<?> mapperClass = e.getMapperClass();
        Method[] methods = mapperClass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getReturnType() == Page.class) {
                String msId = e.getMapperClass().getName() + "." + method.getName();
                boolean withPageParams = false;
                Class<?>[] parameterTypes = method.getParameterTypes();
                self: for (Class<?> clazz : parameterTypes) {
                    if (withPageParams = (clazz == PageParams.class || clazz.getSuperclass() == PageParams.class)) {
                        break self;
                    }
                }
                if (!withPageParams) {
                    throw new MybatisHanlerInitException(String.format("method[%s] returnType is:Page,but not found Parameter[PageParams] in Parameters list", method.getName()));
                }
                pageMappedStatements.put(msId, true);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) MapperMetadata(com.mendmix.mybatis.metadata.MapperMetadata) MybatisHanlerInitException(com.mendmix.mybatis.exception.MybatisHanlerInitException)

Aggregations

MybatisHanlerInitException (com.mendmix.mybatis.exception.MybatisHanlerInitException)2 Method (java.lang.reflect.Method)2 MapperMetadata (com.mendmix.mybatis.metadata.MapperMetadata)1 MapperMethod (com.mendmix.mybatis.metadata.MapperMetadata.MapperMethod)1 Cache (com.mendmix.mybatis.plugin.cache.annotation.Cache)1 Annotation (java.lang.annotation.Annotation)1 Param (org.apache.ibatis.annotations.Param)1