mybatis拦截器

本文介绍了Mybatis拦截器的作用和原理,详细解析了如何定义和使用拦截器,以及拦截器在Mybatis中的注册过程。通过拦截器,开发者可以在Executor的query方法执行前后插入自定义逻辑,增强Mybatis的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

拦截器的一个作用就是我们可以拦截某些方法的调用,我们可以选择在这些被拦截的方法执行前后加上某些逻辑,或者丢弃这些被拦截的方法而执行自己的逻辑。如对于mybatis的Executor,有几种实现:BatchExecutor,ReuseExecutor、SimpleExecutor和CachingExecutor,当这几种Executor接口的query方法无法满足我们的要求的时候,我们就可以建立一个拦截器来实现自己的query方法;拦截器一般采用aop动态实现。

二、拦截器原理

对于mybatis,我们可以通过interceptor接口定义自己的拦截器。interceptor接口定义:

package org.apache.ibatis.plugin;

import java.util.Properties;

public interface Interceptor {

    Object intercept(Invocation invocation) throws Throwable;

    Object plugin(Object target);

    void setProperties(Properties properties);

}

plugin方法主要是用于封装目标对象,通过该方法我们可以决定是否要进行拦截进而决定返回什么样的目标对象。
intercept方法就是要进行拦截的时候执行的方法。setProperties主要用于在配置文件中指定属性,这个方法在Configuration初始化当前的Interceptor时就会执行.在mybatis中有一个plugin类,该类包括静态方法wrap,通过该方法可以决定需要返回的对象是目标对象还是代理。

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;

public class Plugin implements InvocationHandler {

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

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

    public static Object wrap(Object target, Interceptor interceptor) {
        //解析获取需要拦截的类以及方法{*}
        Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
        Class<?> type = target.getClass();
        //解析type是否存在需要拦截的接口{*}
        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
        //决定返回的对象是否为代理{*}
        if (interfaces.length > 0) {
            return Proxy.newProxyInstance(
                type.getClassLoader(),
                interfaces,
                new Plugin(target, interceptor, signatureMap));
        }
        //返回原目标对象
        return target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            Set<Method> methods = signatureMap.get(method.getDeclaringClass());
            //如果当前执行的方法是定义的需要拦截的方法,则把目标对象,要拦截的方法以及参数封装为一个Invocation对象传递给拦截器方法intercept;
            //Invocation中定义了定义了一个proceed方法,其逻辑就是调用当前方法,所以如果在intercept中需要继续调用当前方法的话可以调用invocation的procced方法;
            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);
        }
    }

    //根据注解解析需要拦截的方法
    //两个重要的注解:@Intercepts以及其值其值@Signature(一个数组)
    //@Intercepts用于表明当前的对象是一个Interceptor
    //@Signature则表明要拦截的接口、方法以及对应的参数类型。
    private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
        Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
        if (interceptsAnnotation == null) { // issue #251
            throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
        }
        Signature[] sigs = interceptsAnnotation.value();
        Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();
        for (Signature sig : sigs) {
            Set<Method> methods = signatureMap.get(sig.type());
            if (methods == null) {
                methods = new HashSet<Method>();
                signatureMap.put(sig.type(), methods);
            }
            try {
                Method method = sig.type().getMethod(sig.method(), sig.args());
                methods.add(method);
            } catch (NoSuchMethodException e) {
                throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
            }
        }
        return signatureMap;
    }

    private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>>  signatureMap) {
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        while (type != null) {
            for (Class<?> c : type.getInterfaces()) {
                if (signatureMap.containsKey(c)) {
                    interfaces.add(c);
                }
            }
            type = type.getSuperclass();
        }
        return interfaces.toArray(new Class<?>[interfaces.size()]);
    }
}

三、拦截器实例

package com.mybatis.interceptor;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
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.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts( {
@Signature(method = "query", type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class })}) 
public class TestInterceptor implements Interceptor {
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        return result;
    }

    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    public void setProperties(Properties properties) {
        String p = properties.getProperty("property");
    }
}

首先用@Intercepts标记了这是一个Interceptor,通过@Signatrue设计拦截点:拦截Executor接口中参数类型为MappedStatement、Object、RowBounds和ResultHandler的query方法;intercept方法调用invocation的proceed方法,使当前方法正常调用。

四、拦截器的注册

注册拦截器是通过在Mybatis配置文件中plugins元素下的plugin元素来进行的,Mybatis在注册定义的拦截器时会先把对应拦截器下面的所有property通过Interceptor的setProperties方法注入。如:

<plugins>
    <plugin interceptor="com.mybatis.interceptor.TestInterceptor">
        <property name="property" value="拦截器配置"/>
    </plugin>
</plugins>

 

### MyBatis 拦截器的使用与实现 #### 什么是 MyBatis 拦截器MyBatis 提供了一种灵活的方式来扩展其功能——拦截器Interceptor)。它允许开发人员在 SQL 执行的不同阶段插入自定义逻辑。通过拦截器,可以对 ParameterHandler、StatementHandler、Executor 和 ResultSetHandler 这四种类型的对象进行拦截并修改它们的行为[^1]。 --- #### 截获的目标接口 MyBatis拦截器能够拦截以下四个核心接口中的方法调用: - **`org.apache.ibatis.executor.Executor`**: 主要用于拦截执行器的相关操作,比如 `update()` 或 `query()` 方法。 - **`org.apache.ibatis.executor.statement.StatementHandler`**: 负责处理 SQL 语句的构建和预编译工作。 - **`org.apache.ibatis.executor.parameter.ParameterHandler`**: 处理 SQL 参数绑定的过程。 - **`org.apache.ibatis.executor.resultset.ResultSetHandler`**: 对查询结果集进行解析的操作[^3]。 这些目标接口覆盖了 MyBatis 数据访问的核心流程,使得开发者可以在多个层次上定制化行为。 --- #### 如何创建一个简单的 MyBatis 拦截器? 下面展示了一个基本的拦截器实现: ```java import org.apache.ibatis.plugin.*; import java.util.Properties; @Intercepts({ @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) }) public class ExampleInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { System.out.println("Before update operation..."); Object result = invocation.proceed(); // 继续执行原生方法 System.out.println("After update operation..."); return result; } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); // 将当前拦截器应用到目标对象 } @Override public void setProperties(Properties properties) { // 可在此设置一些属性配置 } } ``` 上述代码展示了如何编写一个拦截器来捕获 `Executor.update()` 方法的调用,并在其前后打印日志消息[^4]。 --- #### 配置拦截器的方式 ##### 方式一:XML 配置方式 如果未启用 Spring 容器管理,则可以通过 XML 文件注册拦截器: ```xml <plugins> <plugin interceptor="com.example.ExampleInterceptor"/> </plugins> ``` ##### 方式二:Spring 自动扫描 当项目基于 Spring Boot 构建时,可以直接将拦截器声明为 Bean 并交由框架托管。只需给拦截器类加上 `@Component` 注解即可自动加载: ```java @Component public class ExampleInterceptor implements Interceptor { ... } ``` 这种方式下无需手动配置插件实例,因为 Spring 会在初始化过程中完成注入。 --- #### 实际应用场景举例 以下是几个常见的场景及其对应的解决方案说明: 1. **分页查询** - 使用拦截器动态改写 SQL 查询语句,在原有基础上附加 LIMIT 子句以支持分页功能[^2]。 2. **性能监控** - 在 StatementHandler 层面记录每条 SQL 的耗时情况,便于后续分析优化瓶颈所在。 3. **多租户隔离** - 动态向所有 SELECT/UPDATE/DML 类型的 SQL 中追加特定条件字段(如 TenantId),从而保障跨账户间的数据安全边界。 4. **缓存控制** - 借助 Executor 接口重载部分更新逻辑,减少不必要的数据库交互次数;或者引入第三方分布式缓存组件作为补充存储介质。 --- #### 注意事项 尽管拦截器提供了强大的能力,但在设计时仍需注意以下几点原则: - 不应过度依赖于拦截器解决复杂业务需求; - 修改原始 SQL 结构可能带来潜在风险,务必充分测试验证兼容性; - 如果涉及事务上下文中敏感数据变更,请格外小心避免破坏一致性约束。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值