1、自定义拦截器
package com.tigeriot.mqtt.config.mybatis;
import cn.hutool.json.JSONUtil;
import com.tigeriot.mqtt.common.CacheDev;
import com.tigeriot.mqtt.common.CacheMQ;
import com.tigeriot.mqtt.entity.log.DevLog;
import com.tigeriot.mqtt.service.DevLogService;
import com.tigeriot.mqtt.util.JsonUtils;
import com.tigeriot.mqtt.util.SQLParseUtils;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Properties;
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
public class OperationLogInterceptor implements Interceptor{
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = invocation.getArgs()[1];
BoundSql boundSql = mappedStatement.getBoundSql(parameter);
String sql = boundSql.getSql();
String tableName = SQLParseUtils.getTableName(sql);
throw new Exception("表名解析失败");
if (tableName.equalsIgnoreCase("DevLog")){
return invocation.proceed();
long start =System.currentTimeMillis();
Object result = invocation.proceed();
long end =System.currentTimeMillis();
String param = JsonUtils.getJson(parameter);
System.out.println("json: "+param);
String sqlType = SQLParseUtils.parseSQLType(sql);
throw new Exception("日志SQL解析失败");
sql= sql.replaceAll("\n", "");
Date createTime =new Date();
Long executeTime=end-start;
DevLog devLog = new DevLog(null, tableName, sqlType, sql, param, executeTime, createTime);
System.out.println("devLog:"+devLog);
boolean offer = CacheMQ.logQueue.offer(devLog);
System.out.println("成功放入队列,异步执行日志存入数据库");
public Object plugin(Object target) {
return Plugin.wrap(target, this);
public void setProperties(Properties properties) {

2、mybatis-config.xml配置文件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<plugin interceptor="com.tigeriot.mqtt.config.mybatis.OperationLogInterceptor">
3、springboot配置 这个 mybatis-config.xml
configLocation: classpath:mybatis/mybatis-config.xml
4、SQL工具类
package com.tigeriot.mqtt.util;
import cn.hutool.json.JSONUtil;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class SQLParseUtils {
public static String parseSQLType(String sql){
String type = sql.split(" ")[0];
if (type.equalsIgnoreCase("INSERT")){
} else if (type.equalsIgnoreCase("UPDATE")) {
} else if (type.equalsIgnoreCase("DELETE")) {
public static String getTableName(String sql) {
String regex = "(?i)(?:INSERT\\s+INTO|UPDATE|DELETE\\s+FROM)\\s+(\\w+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql.trim());
tableName = matcher.group(1);
public static String getParam(String json,String type) throws Exception {
public static void main(String[] args) {
String tableName = getTableName("INSERT INTO orders (customer_id, product_id, quantity) VALUES (789, 123, 5)");
System.out.println(tableName);
