mybatis拦截器的设计思想

mybatis拦截器的设计思想(分页的使用)

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.github.pagehelper;

import com.github.pagehelper.cache.Cache;
import com.github.pagehelper.cache.CacheFactory;
import com.github.pagehelper.util.MSUtils;
import com.github.pagehelper.util.StringUtil;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts({@Signature(
type = Executor.class,
method = “query”,
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
type = Executor.class,
method = “query”,
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class PageInterceptor implements Interceptor {
protected Cache<String, MappedStatement> msCountMap = null;
private Dialect dialect;
private String default_dialect_class = “com.github.pagehelper.PageHelper”;
private Field additionalParametersField;
private String countSuffix = “_COUNT”;

public PageInterceptor() {
}

//拦截到方法的具体i实现 invocation这个包括了主调类,方法,参数
public Object intercept(Invocation invocation) throws Throwable {
try {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement)args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds)args[2];
ResultHandler resultHandler = (ResultHandler)args[3];
Executor executor = (Executor)invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
cacheKey = (CacheKey)args[4];
boundSql = (BoundSql)args[5];
}

        List resultList;
        if (this.dialect.skip(ms, parameter, rowBounds)) {
            resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        } else {
            String msId = ms.getId();
            Configuration configuration = ms.getConfiguration();
            Map<String, Object> additionalParameters = (Map)this.additionalParametersField.get(boundSql);
            if (this.dialect.beforeCount(ms, parameter, rowBounds)) {
                String countMsId = msId + this.countSuffix;
                MappedStatement countMs = this.getExistedMappedStatement(configuration, countMsId);
                Long count;
                if (countMs != null) {
                    count = this.executeManualCount(executor, countMs, parameter, boundSql, resultHandler);
                } else {
                    countMs = (MappedStatement)this.msCountMap.get(countMsId);
                    if (countMs == null) {
                        countMs = MSUtils.newCountMappedStatement(ms, countMsId);
                        this.msCountMap.put(countMsId, countMs);
                    }

                    count = this.executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);
                }

                if (!this.dialect.afterCount(count, parameter, rowBounds)) {
                    Object var24 = this.dialect.afterPage(new ArrayList(), parameter, rowBounds);
                    return var24;
                }
            }

            if (!this.dialect.beforePage(ms, parameter, rowBounds)) {
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
            } else {
                parameter = this.dialect.processParameterObject(ms, parameter, boundSql, cacheKey);
                String pageSql = this.dialect.getPageSql(ms, boundSql, parameter, rowBounds, cacheKey);
                BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter);
                Iterator var17 = additionalParameters.keySet().iterator();

                while(true) {
                    if (!var17.hasNext()) {
                        resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, pageBoundSql);
                        break;
                    }

                    String key = (String)var17.next();
                    pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
            }
        }

        Object var22 = this.dialect.afterPage(resultList, parameter, rowBounds);
        return var22;
    } finally {
        this.dialect.afterAll();
    }
}

private Long executeManualCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    BoundSql countBoundSql = countMs.getBoundSql(parameter);
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
    Long count = ((Number)((List)countResultList).get(0)).longValue();
    return count;
}

private Long executeAutoCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
    Map<String, Object> additionalParameters = (Map)this.additionalParametersField.get(boundSql);
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    String countSql = this.dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);
    BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
    Iterator var11 = additionalParameters.keySet().iterator();

    while(var11.hasNext()) {
        String key = (String)var11.next();
        countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
    }

    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
    Long count = (Long)((List)countResultList).get(0);
    return count;
}

private MappedStatement getExistedMappedStatement(Configuration configuration, String msId) {
    MappedStatement mappedStatement = null;

    try {
        mappedStatement = configuration.getMappedStatement(msId, false);
    } catch (Throwable var5) {
        ;
    }

    return mappedStatement;
}

//mybatis自带的代理类实现的方法
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

public void setProperties(Properties properties) {
    this.msCountMap = CacheFactory.createCache(properties.getProperty("msCountCache"), "ms", properties);
    String dialectClass = properties.getProperty("dialect");
    if (StringUtil.isEmpty(dialectClass)) {
        dialectClass = this.default_dialect_class;
    }

    try {
        Class<?> aClass = Class.forName(dialectClass);
        this.dialect = (Dialect)aClass.newInstance();
    } catch (Exception var6) {
        throw new PageException(var6);
    }

    this.dialect.setProperties(properties);
    String countSuffix = properties.getProperty("countSuffix");
    if (StringUtil.isNotEmpty(countSuffix)) {
        this.countSuffix = countSuffix;
    }

    try {
        this.additionalParametersField = BoundSql.class.getDeclaredField("additionalParameters");
        this.additionalParametersField.setAccessible(true);
    } catch (NoSuchFieldException var5) {
        throw new PageException(var5);
    }
}

}
主要介绍plugin的这个方法,上述是拦截器里面具体的实现,下面是plugin的实现

/**

  • Copyright 2009-2017 the original author or authors.
  • Licensed under the Apache License, Version 2.0 (the “License”);
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •   http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an “AS IS” BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */
    package org.apache.ibatis.plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.ibatis.reflection.ExceptionUtil;

/**

  • @author sun
    */
    //实现了invocationHandler接口,就可以成为jdk动态代理的处理器
    public class Plugin implements InvocationHandler {

private final Object target;
private final Interceptor interceptor;
private final Map<Class<?>, Set> signatureMap;

private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}

//首先调用这个方法,返回拦截方法的代理类还是累的本身
public static Object wrap(Object target, Interceptor interceptor) {
Map<Class<?>, Set> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
//如果方法而已被拦截就返回代理类,如果不是返回本身
if (interfaces.length > 0) {
//jdk的动态代理
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set methods = signatureMap.get(method.getDeclaringClass());
//判断被拦截的方法是否被包含在拦截器应该拦截的方法中,如果存在调用自定义拦截器的intercept方法,不存在就调用自己方法自己invoke()方法
if (methods != null && methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
//这个是将拦截器中应该被拦截的类的方法,放入一个map集合中
private static Map<Class<?>, Set> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// issue #251
if (interceptsAnnotation == null) {
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();
Map<Class<?>, Set> signatureMap = new HashMap

private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set> signatureMap) {
Set<Class<?>> interfaces = new HashSet

}
本人是一个新手,可能写的东西只是适合自己看,写这个文章的目的也是主要记录一下自己学习过程中学习到的新的知识,有问题的话,请给我修正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值