Search in sources :

Example 1 with InterceptorHandler

use of com.mendmix.mybatis.core.InterceptorHandler in project jeesuite-libs by vakinge.

the class JeesuiteMybatisInterceptor method afterRegister.

public void afterRegister() {
    Iterator<InterceptorHandler> it = interceptorHandlers.iterator();
    while (it.hasNext()) {
        InterceptorHandler handler = it.next();
        handler.start(this);
    }
}
Also used : InterceptorHandler(com.mendmix.mybatis.core.InterceptorHandler)

Example 2 with InterceptorHandler

use of com.mendmix.mybatis.core.InterceptorHandler in project jeesuite-libs by vakinge.

the class JeesuiteMybatisInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    InvocationVals invocationVal = new InvocationVals(invocation);
    Object result = null;
    boolean proceed = false;
    try {
        for (InterceptorHandler handler : interceptorHandlers) {
            result = handler.onInterceptor(invocationVal);
            if (result != null) {
                proceed = handler.getClass() == SqlRewriteHandler.class;
                break;
            }
        }
        if (result == null) {
            result = invocation.proceed();
            proceed = true;
        }
        return result;
    } finally {
        for (InterceptorHandler handler : interceptorHandlers) {
            try {
                handler.onFinished(invocationVal, proceed ? result : null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : InterceptorHandler(com.mendmix.mybatis.core.InterceptorHandler) SqlRewriteHandler(com.mendmix.mybatis.plugin.rewrite.SqlRewriteHandler)

Example 3 with InterceptorHandler

use of com.mendmix.mybatis.core.InterceptorHandler in project jeesuite-libs by vakinge.

the class JeesuiteMybatisInterceptor method setInterceptorHandlers.

private void setInterceptorHandlers(String[] hanlderNames) {
    for (String name : hanlderNames) {
        if (org.apache.commons.lang3.StringUtils.isBlank(name))
            continue;
        if (CacheHandler.NAME.equals(name)) {
            this.interceptorHandlers.add(new CacheHandler());
            cacheEnabled = true;
        } else if (RwRouteHandler.NAME.equals(name)) {
            this.interceptorHandlers.add(new RwRouteHandler());
            rwRouteEnabled = true;
        } else {
            // 自定义的拦截器
            try {
                Class<?> clazz = Class.forName(name);
                InterceptorHandler handler = (InterceptorHandler) clazz.newInstance();
                this.interceptorHandlers.add(handler);
                logger.info("registered cumstom InterceptorHandler:{}", name);
            } catch (Exception e) {
                logger.error("registered cumstom InterceptorHandler error", e);
            }
        }
    }
    // 排序 分页和数据权限会重写sql,所以不走缓存,所以放在缓存之前
    Collections.sort(this.interceptorHandlers, new Comparator<InterceptorHandler>() {

        @Override
        public int compare(InterceptorHandler o1, InterceptorHandler o2) {
            return Integer.compare(o1.interceptorOrder(), o2.interceptorOrder());
        }
    });
}
Also used : InterceptorHandler(com.mendmix.mybatis.core.InterceptorHandler) RwRouteHandler(com.mendmix.mybatis.plugin.rwseparate.RwRouteHandler) CacheHandler(com.mendmix.mybatis.plugin.cache.CacheHandler)

Aggregations

InterceptorHandler (com.mendmix.mybatis.core.InterceptorHandler)3 CacheHandler (com.mendmix.mybatis.plugin.cache.CacheHandler)1 SqlRewriteHandler (com.mendmix.mybatis.plugin.rewrite.SqlRewriteHandler)1 RwRouteHandler (com.mendmix.mybatis.plugin.rwseparate.RwRouteHandler)1