Mr. Cappuccino的第56杯咖啡——Mybatis拦截器

Mybatis拦截器

概述

Mybatis允许使用者在映射语句执行过程中的某一些指定的节点进行拦截调用,通过织入拦截器,在不同节点修改一些执行过程中的关键属性,从而影响SQL的生成、执行和返回结果。

默认情况下,Mybatis支持四种对象拦截:

  1. Executor:拦截执行器的方法;
  2. ParameterHandler:拦截参数的处理;
  3. ResultSetHandler:拦截结果集的处理;
  4. StatementHandler:拦截Sql语法构建的处理;

执行顺序:Executor => StatementHandler => ParameterHandler => ResultSetHandler

注:本文代码基于《Mybatis一级缓存&二级缓存》中的"一级缓存(Spring整合Mybatis)"的代码进行调整。

应用场景

  1. 分页查询;
  2. 数据脱敏;
  3. 数据过滤;
  4. 监控Sql语句执行耗时;

项目结构

实现分页查询

StatementInterceptor.java

java 复制代码
package com.mybatis.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author honey
 * @date 2023-08-02 15:25:26
 */
@Slf4j
@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
})
@Component
public class StatementInterceptor implements Interceptor {

    @Value("${mybatis.page-helper.rule}")
    private String rule;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        // 判断sql语句的类型
        switch (sqlCommandType) {
            case SELECT:
                extendLimit(statementHandler);
                break;
            case INSERT:
            case UPDATE:
            case DELETE:
            case FLUSH:
            default:
                break;
        }
        log.info("【StatementInterceptor】方法拦截前执行");
        Object result = invocation.proceed();
        log.info("【StatementInterceptor】方法拦截后执行");
        return result;
    }

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

    @Override
    public void setProperties(Properties properties) {

    }

    private void extendLimit(StatementHandler statementHandler) throws NoSuchFieldException, IllegalAccessException {
        // 获取原生sql语句
        BoundSql boundSql = statementHandler.getBoundSql();
        Class<? extends BoundSql> aClass = boundSql.getClass();
        // 使用反射机制修改原生sql语句
        Field sql = aClass.getDeclaredField("sql");
        sql.setAccessible(true);
        String oldSqlStr = boundSql.getSql();
        log.info("原生sql语句:{}", oldSqlStr);
        // 加上分页规则
        sql.set(boundSql, oldSqlStr + " " + rule);
    }
}

application.yml

yaml 复制代码
mybatis:
  page-helper:
    rule: limit 2

其它拦截器的使用

ExecutorInterceptor.java

java 复制代码
package com.mybatis.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
 * @author honey
 * @date 2023-08-02 18:11:28
 */
@Slf4j
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
@Component
public class ExecutorInterceptor implements Interceptor {

    Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("【ExecutorInterceptor】方法拦截前执行");
        Object result = invocation.proceed();
        log.info("【ExecutorInterceptor】方法拦截后执行");
        return result;
    }

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

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

ParameterInterceptor.java

java 复制代码
package com.mybatis.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.util.Properties;

/**
 * @author honey
 * @date 2023-08-02 18:22:15
 */
@Slf4j
@Intercepts({
        @Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class})
})
@Component
public class ParameterInterceptor implements Interceptor {

    Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("【ParameterInterceptor】方法拦截前执行");
        Object result = invocation.proceed();
        log.info("【ParameterInterceptor】方法拦截后执行");
        return result;
    }

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

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

ResultSetInterceptor.java

java 复制代码
package com.mybatis.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.sql.Statement;
import java.util.Properties;

/**
 * @author honey
 * @date 2023-08-02 18:24:00
 */
@Slf4j
@Intercepts({
        @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
})
@Component
public class ResultSetInterceptor implements Interceptor {

    Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("【ResultSetInterceptor】方法拦截前执行");
        Object result = invocation.proceed();
        log.info("【ResultSetInterceptor】方法拦截后执行");
        return result;
    }

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

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
相关推荐
huohuopro2 小时前
Mybatis的七种传参方式
java·开发语言·mybatis
sunnyday04266 小时前
Spring Boot 自定义 Starter 实战:从创建到使用的完整指南
spring boot·后端·mybatis
小北方城市网6 小时前
Redis 分布式锁与缓存三大问题解决方案
spring boot·redis·分布式·后端·缓存·wpf·mybatis
Dontla10 小时前
Mybatis Introduction (Java ORM Framework)
java·开发语言·mybatis
Knight_AL13 小时前
Spring Boot 多模块项目中优雅实现自动配置(基于 AutoConfiguration.imports)
java·spring boot·mybatis
indexsunny13 小时前
互联网大厂Java面试实战:从Spring Boot到微服务架构的三轮提问
java·spring boot·微服务·eureka·kafka·mybatis·spring security
Dontla14 小时前
boilerplate Introduction样板代码介绍(raw JDBC,Mybatis,productivity killer)
mybatis
没有bug.的程序员15 小时前
Spring Boot 数据访问:JPA 与 MyBatis 集成对比与性能优化深度解密
java·spring boot·性能优化·mybatis·jpa·集成对比
派大鑫wink15 小时前
【Day47】MyBatis 进阶:动态 SQL、关联查询(一对一 / 一对多)
数据库·sql·mybatis
派大鑫wink15 小时前
【Day48】MyBatis 注解开发:替代 XML 映射文件
xml·java·mybatis