mybatis plus intercept修改sql

news2024/9/22 23:29:11

原始需求

在SQL语句前面加上一个request-id

问题描述

今天收到业务同学反馈,说接入某个SDK后,request-id本地debug发现sql已经修改了,但打印的sql中却没有request-id信息

在这里插入图片描述

在这里插入图片描述

看了下代码,发现用户的代码其实就是下方 方案一代码,取不到的原因也在代码中注释了

方案一

package com.jiankunking.mybatisplus;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.jiankunking.utils.JkkLogUtil;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
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.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(type = StatementHandler.class, method = "prepare",
            args = {Connection.class, Integer.class}),
        @Signature(type = StatementHandler.class, method = "getBoundSql", args = {}),
        @Signature(type = Executor.class, method = "update",
            args = {MappedStatement.class, Object.class}),
        @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}),
    }
)
@Slf4j
public class MybatisPlusRequestIdInterceptor extends MybatisPlusInterceptor {
  private Boolean enabled = true;

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    Object target = invocation.getTarget();
    Object[] args = invocation.getArgs();
    BoundSql boundSql = null;
    if (target instanceof Executor) {
      Object parameter = args[1];
      boolean isUpdate = args.length == 2;
      MappedStatement ms = (MappedStatement) args[0];
      if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {
        RowBounds rowBounds = (RowBounds) args[2];
        if (args.length == 4) {
          boundSql = ms.getBoundSql(parameter);
        } else {
          // 几乎不可能走进这里面,除非使用Executor的代理对象调用query[args[6]]
          boundSql = (BoundSql) args[5];
        }
      }
    } else {
      StatementHandler statementHandler = (StatementHandler) target;
      boundSql = statementHandler.getBoundSql();
    }

    if (enabled && boundSql != null) {
      String sql = boundSql.getSql();
      String requestId = JkkLogUtil.getCurrentRequestId();
      StringBuilder sb = new StringBuilder("/*");
      if (isNotBlank(requestId) && isNotBlank(sql)) {
        sb.append(" Jkk-request-id:").append(requestId).append(" ");
      }

      String armsTraceId = JkkLogUtil.getTraceId();
      if (isNotBlank(armsTraceId) && isNotBlank(sql)) {
        sb.append(" trace-id:").append(armsTraceId).append(" ");
      }

      String armsRpcId = JkkLogUtil.getRpcId();
      if (isNotBlank(armsRpcId)) {
        sb.append(" rpc-id:").append(armsRpcId).append(" ");
      }
      sb.append(" */");

      sb.append(sql);
      // 通过反射修改sql语句
      Field field = boundSql.getClass().getDeclaredField("sql");
      field.setAccessible(true);
      field.set(boundSql, sb.toString());
      // 这里如果不调用 return invocation.proceed();
      // 而是执行父类intercept的话,会导致反射修改的SQL无效
      // 因为父类获取sql还是基于参数再次拼接的没有直接使用boundSql
    }

    return super.intercept(invocation);
  }

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

  @Override
  public void setProperties(Properties properties) {
    enabled = Boolean.valueOf(properties.getProperty("enabled", "true"));
  }
}

方案二

直接修改mybatisplus的Statement

package com.jiankunking.mybatisplus;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.jiankunking.utils.JkkLogUtil;

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

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.*;
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.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts(
        {
                @Signature(type = StatementHandler.class, method = "prepare",
                        args = {Connection.class, Integer.class}),
                @Signature(type = StatementHandler.class, method = "getBoundSql", args = {}),
                @Signature(type = Executor.class, method = "update",
                        args = {MappedStatement.class, Object.class}),
                @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}),
        }
)
@Slf4j
public class MybatisPlusRequestIdInterceptor extends MybatisPlusInterceptor {
    private Boolean enabled = true;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget();
        Object[] args = invocation.getArgs();
        BoundSql boundSql = null;
        if (target instanceof Executor) {
            Object parameter = args[1];
            boolean isUpdate = args.length == 2;
            MappedStatement ms = (MappedStatement) args[0];
            if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {
                RowBounds rowBounds = (RowBounds) args[2];
                if (args.length == 4) {
                    boundSql = ms.getBoundSql(parameter);
                } else {
                    // 几乎不可能走进这里面,除非使用Executor的代理对象调用query[args[6]]
                    boundSql = (BoundSql) args[5];
                }
            }
        } else {
            StatementHandler statementHandler = (StatementHandler) target;
            boundSql = statementHandler.getBoundSql();
        }

        if (enabled && boundSql != null) {
            String sql = boundSql.getSql();
            String requestId = JkkLogUtil.getCurrentRequestId();
            StringBuilder sb = new StringBuilder("/*");
            if (isNotBlank(requestId) && isNotBlank(sql)) {
                sb.append(" Jkk-request-id:").append(requestId).append(" ");
            }

            String armsTraceId = JkkLogUtil.getTraceId();
            if (isNotBlank(armsTraceId) && isNotBlank(sql)) {
                sb.append(" trace-id:").append(armsTraceId).append(" ");
            }

            String armsRpcId = JkkLogUtil.getRpcId();
            if (isNotBlank(armsRpcId)) {
                sb.append(" rpc-id:").append(armsRpcId).append(" ");
            }
            sb.append(" */");

            sb.append(sql);
            // 通过反射修改sql语句
            //Field field = boundSql.getClass().getDeclaredField("sql");
            //field.setAccessible(true);
            //field.set(boundSql, sb.toString());

            if (target instanceof Executor) {
                setCurrentSql(invocation, sb.toString());
            } else {
                StatementHandler statementHandler = (StatementHandler) target;
                MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
                metaObject.setValue("delegate.boundSql.sql", sb.toString());
            }
        }

        return super.intercept(invocation);
    }

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

    @Override
    public void setProperties(Properties properties) {
        enabled = Boolean.valueOf(properties.getProperty("enabled", "true"));
    }

    private static final int MAPPED_STATEMENT_INDEX = 0;
    private static final int PARAM_OBJ_INDEX = 1;

    private void setCurrentSql(Invocation invocation, String sql) {
        Object[] args = invocation.getArgs();
        Object paramObj = args[PARAM_OBJ_INDEX];

        MappedStatement mappedStatement = (MappedStatement) args[MAPPED_STATEMENT_INDEX];

        BoundSql boundSql = mappedStatement.getBoundSql(paramObj);
        MappedStatement newMappedStatement = copyFromMappedStatement(
                mappedStatement, new BoundSqlSqlSource(boundSql));
        MetaObject metaObject = MetaObject.forObject(newMappedStatement,
                new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),
                new DefaultReflectorFactory());
        metaObject.setValue("sqlSource.boundSql.sql", sql);
        args[MAPPED_STATEMENT_INDEX] = newMappedStatement;
    }

    private class BoundSqlSqlSource implements SqlSource {
        BoundSql boundSql;

        public BoundSqlSqlSource(BoundSql boundSql) {
            this.boundSql = boundSql;
        }

        public BoundSql getBoundSql(Object parameterObject) {
            return boundSql;
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    private MappedStatement copyFromMappedStatement(MappedStatement ms,
                                                    SqlSource newSqlSource) {
        MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms
                .getId(), newSqlSource, ms.getSqlCommandType());
        builder.resource(ms.getResource());
        builder.fetchSize(ms.getFetchSize());
        builder.statementType(ms.getStatementType());
        builder.keyGenerator(ms.getKeyGenerator());
        // setStatementTimeout()
        builder.timeout(ms.getTimeout());
        // setParameterMap()
        builder.parameterMap(ms.getParameterMap());
        // setStatementResultMap()
        List<ResultMap> resultMaps = new ArrayList<ResultMap>();
        String id = "-inline";
        if (ms.getResultMaps() != null) {
            id = ms.getResultMaps().get(0).getId() + "-inline";
        }
        ResultMap resultMap = new ResultMap.Builder(null, id, Long.class,
                new ArrayList()).build();
        resultMaps.add(resultMap);
        builder.resultMaps(resultMaps);
        builder.resultSetType(ms.getResultSetType());
        // setStatementCache()
        builder.cache(ms.getCache());
        builder.flushCacheRequired(ms.isFlushCacheRequired());
        builder.useCache(ms.isUseCache());
        return builder.build();
    }

    //private MappedStatement getMappedStatement(Invocation invo) {
    //    Object[] args = invo.getArgs();
    //    Object mappedStatement = args[MAPPED_STATEMENT_INDEX];
    //    return (MappedStatement) mappedStatement;
    //}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1543235.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【C++进阶】竞赛常用库函数

一、排序 sort简介 sort函数包含在头文件<algorithm>中。在使用前需要#include <algorithm>或使用万能头文件。sort是C标准库中的一个函数模板&#xff0c;用于对指定范围内的元素进行排序。sort算法使用的是快速排序(QuickSort)或者类似快速排序的改进算法&…

OCP NVME SSD规范解读-14.Firmware固件升级要求

4.11节 Firmware Update Requirements 描述了数据中心NVMe SSD固件更新的具体要求&#xff0c;确保固件升级过程既安全又可靠&#xff0c;同时充分考虑了设备在升级过程中的可用性和功能性。 FWUP-1: 设备必须记录每一次固件激活过程。这意味着固件升级过程中&#xff0c;设备会…

一文搞懂 YOLOv9 训练推理全流程 | YOLOv9你绝对不知道的细节!

文章地址&#xff1a;https://arxiv.org/pdf/2402.13616.pdf 代码地址&#xff1a;https://github.com/WongKinYiu/yolov9 前言 在这篇博客中&#xff0c;我们来聊聊 YOLOv9。首先&#xff0c;值得注意的一点是&#xff0c;YOLOv9的变化相对较小&#xff0c;它仍然基于YOLOv5的…

在线影院项目话术(0.7w精选)

项目介绍 这个电影院项目不同于常见的基于会员限制用户观看范围的在线影院项目&#xff0c;主要就是按需购买片源来进行观看&#xff0c;用户就不会因高额的会员费而劝退。 项目的主要实现就是&#xff1a;微服务的五大主键&#xff0c;数据库使用mysql,redis&#xff0c;中间…

2024年湖北荆门市工程系列职称评审开始啦

(一)中级测试对象&#xff1a;拟参加近两年度相关系列(专业)中级技术职务评审&#xff0c;且未取得中级水平能力测试合格证书的专业技术人员。 (二)初级测试对象&#xff1a;拟参加近两年度档案专业、工程系列助理级职称评审&#xff0c;且未取得初级水平能力测试合格证书的专业…

白酒:陈酿过程中的老熟度评价与品质提升方法

在豪迈白酒的酿造过程中&#xff0c;陈酿是一个至关重要的环节。陈酿不仅能使白酒老熟&#xff0c;提品质&#xff0c;还能发展出与众不同的风味和口感。云仓酒庄深知陈酿的重要性&#xff0c;并进行了深入的研究和实践。本文将探讨陈酿过程中的老熟度评价与品质提升方法。 首先…

蓝桥杯day7刷题日记

P8697 [蓝桥杯 2019 国 C] 最长子序列 思路&#xff1a;直接遍历&#xff0c;和子序列相同就记录&#xff0c;不然就下一位 #include <iostream> #include <string> using namespace std; int res;int main() {string s,t;cin>>s>>t;int i0,j0;while…

IDEA 远程调试

1.什么是远程调试 Java提供了一个远程调试功能&#xff0c;支持设置断点及线程级的调试同时&#xff0c;不同的JVM通过接口的协议联系&#xff0c;本地的Java文件在远程JVM建立联系和通信。 2.服务端开启远程调试 开启远程调试功能&#xff0c;需要修改tomcat 的catalina.sh…

目录框架【欢迎订阅收藏】

前端篇 第一章vue.js 1.Vue.js入门及环境搭建 2.vue.js基础知识 3.Vue.js自定义组件 Vue.js路由 5.Vue.js状态管理 6.网络通信axios 第二章Tailwindcss 1.tailwindcss介绍及引入 2.基础样式 3.组件样式 4.工具样式 第三章Nuxt 1.nuxt介绍及环境搭建 2.nuxt 目录结构 3.nuxt …

ElasticSearch文档批量操作[ES系列] - 第503篇

历史文章&#xff08;文章累计500&#xff09; 《国内最全的Spring Boot系列之一》 《国内最全的Spring Boot系列之二》 《国内最全的Spring Boot系列之三》 《国内最全的Spring Boot系列之四》 《国内最全的Spring Boot系列之五》 《国内最全的Spring Boot系列之六》 《…

超声波清洗机怎么选?过来人推荐四大表现突出的宝藏眼镜清洗机!

如果你清洁眼镜的时候总是习惯用纸巾或者是衣服擦的话&#xff0c;我劝你要赶紧把这个坏习惯给改掉&#xff01;很多不知道原因的朋友会问&#xff0c;为什么不能用纸巾擦眼镜&#xff1f;其实是因为纸巾还有衣服布料上&#xff0c;存在一些比较粗糙的微粒&#xff0c;人体手部…

车载测试 HIL测试详解

HIL测试&#xff1a;即硬件在环测试&#xff0c;是一种广泛应用于汽车电子控制系统领域的测试方法。它将实际的硬件&#xff08;如ECU、传感器、执行器等&#xff09;与模拟器件&#xff08;如模型、仿真器等&#xff09;通过接口连接起来&#xff0c;模拟实际的操作环境&#…

动态规划-----最长公共子序列(及其衍生问题)

目录 一.最长公共子序列的基本概念&#xff1a; 解决动态规划问题的一般思路&#xff08;三大步骤&#xff09;&#xff1a; 二.最长公共子序列题目&#xff1a; 三.字符串的删除操作&#xff1a; 四.最小 ASCII 删除和&#xff1a; 一.最长公共子序列的基本概念&#xff…

微前端——qiankun

一、微前端 微前端是指存在于浏览器中的微服务&#xff0c;其借鉴了后端微服务的架构理念&#xff0c;将微服务的概念扩展到前端。即将一个大型的前端应用拆分为成多个模块&#xff0c;每个微前端模块可以有不同的团队开发并进行管理&#xff0c;且可以自主选择框架&#xff0…

seata测试demo(订单)

seata工作流程: seata对分布式事务的协调和控制就是31 1>XID&#xff1a;XID是全局事务的唯一标识&#xff0c;它可以在服务的调用链路中传递&#xff0c;绑定到服务的事务上下文中。 3>TC->TM->RM TC:事务协调器>就是seata 负责维护全局事务和分支事务的状…

选项式API和组合式API的区别

选项式(options) API 和组合式(composition) API两种不同的风格书写&#xff0c;Vue3 的组件可以使用这两种api来编写。 选项式API和组合式API的区别 选项式API 选项式 API&#xff0c;具有相同功能的放在一起&#xff0c;可以用包含多个选项的对象来描述组件的逻辑&…

【周总结】

周总结 完成项目混合版时区改造 完成相关jira问题的修改 完成老版本APP数据保存接口的兼容&#xff0c;手动赋值时区 2024/03/24 天气阴 一点不冷 1.Its time to go、Spring is coming&#xff01; 2. Its a nice day that staying with friends in a peaceful …

初探Ruby编程语言

文章目录 引言一、Ruby简史二、Ruby特性三、安装Ruby四、命令行执行Ruby五、Ruby的编程模型六、案例演示结语 引言 大家好&#xff0c;今天我们将一起探索一门历史悠久、充满魅力的编程语言——Ruby。Ruby是由松本行弘&#xff08;Yukihiro Matsumoto&#xff09;于1993年发明…

LangChain核心模块 Retrieval——文档加载器

Retrieval ​ 许多LLM申请需要用户的特定数据&#xff0c;这些数据不属于模型训练集的一部分&#xff0c;实现这一目标的主要方法是RAG(检索增强生成)&#xff0c;在这个过程中&#xff0c;将检索外部数据&#xff0c;然后在执行生成步骤时将其传递给LLM。 ​ LangChain 提供…

Linux系统安装openGauss结合内网穿透实现公网访问本地数据库管理系统——“cpolar内网穿透”

文章目录 前言1. Linux 安装 openGauss2. Linux 安装cpolar3. 创建openGauss主节点端口号公网地址4. 远程连接openGauss5. 固定连接TCP公网地址6. 固定地址连接测试 前言 openGauss是一款开源关系型数据库管理系统&#xff0c;采用木兰宽松许可证v2发行。openGauss内核深度融合…