当前位置: 首页 > news >正文

通过mybatis的拦截器对SQL进行打标

1、背景

在我们开发的过程中,一般需要编写各种SQL语句,万一生产环境出现了慢查询,那么我们如何快速定位到底是程序中的那个SQL出现的问题呢?

2、解决方案

如果我们的数据访问层使用的是mybatis的话,那么我们可以通过mybatis提供的拦截器拦截系统中的SQL,然后将 mapper的命名空间和id追加到原始SQL的末尾,当作一个注释,这样不就可以实现吗? 类似效果如下:

select * from customer where phone = 'aaaaa';/**com.huan.study.mybatis.mappers.CustomerMapper.findCustomer*/

3、核心实现步骤

1、拦截器拦截SQL进行打标

package com.huan.study.mybatis.plugin;

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.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;

/**
 * 在原始的Sql语句后面追加 sql id,方面知道当前查询语句是那个mapper文件中的
 *
 * @author huan
 * @date 2025/3/11 - 00:30
 */
@Slf4j
@Intercepts(
        {
                @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
        }
)
public class PrintSqlIdInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaStatementHandler = MetaObject.forObject(statementHandler,
                SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
                SystemMetaObject.NULL_META_OBJECT.getReflectorFactory());
        BoundSql boundSql = statementHandler.getBoundSql();
        MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
        String id = mappedStatement.getId();
        log.info("sql语句的id : {}", id);

        String sql = boundSql.getSql();
        if (!sql.endsWith(";")) {
            sql += ";";
        }
        sql = sql + "/**" + id + "*/";

        metaStatementHandler.setValue("delegate.boundSql.sql", sql);
        return invocation.proceed();
    }

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

2、配置插件

mybatis-config.xml中进行插件的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <plugin interceptor="com.huan.study.mybatis.plugin.PrintSqlIdInterceptor"/>
    </plugins>

</configuration>

4、实现效果如下

select * from customer where phone = 'aaaaa';/**com.huan.study.mybatis.mappers.CustomerMapper.findCustomer*/
insert into customer(phone,address) values ('12345','湖北');/**com.huan.study.mybatis.mappers.CustomerMapper.addCustomer*/

可以看到我们对每个SQL都进行了打标,方便SQL的追踪

5、完整代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/mybatis/mybatis-sql-marking

相关文章:

  • 【GIT】什么是GitHub Actions ?
  • 【数据分享】1999—2023年地级市地方一般公共预算收支状况数据(科学技术支出/教育支出等)
  • python--面试题--基础题
  • Python的字符串优雅优化策略:特定编码 -> Unicode码点 -> UTF-8(可自定义)
  • MySQL的事务机制
  • Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletContext
  • 每日一题——点击消除
  • 从零基础到能独立设计单片机产品,一般需要经历哪些学习阶段?
  • 网页制作代码html制作一个网页模板
  • [S32K]SPI
  • 结构型模式之适配器模式:让不兼容的接口兼容
  • 某乎x-zse-96加密算法分析与还原
  • 落雪音乐Pro 8.8.6 | 内置8条音源,无需手动导入,纯净无广告
  • Go string 字符串底层逻辑
  • MessageBox()
  • AI 实战 - 基于2D图片的三维人脸重建
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(22):给与和得到相关用法
  • 力扣——146.LRU缓存
  • 【AD】5-19 PCB阻焊开窗及异形开窗
  • 五、Ultra-Fast-Lane-Detection 训练数据集转换
  • 重庆城市轨道交通拟听证调价:公布两套票价方案,正征求意见
  • 男子聚餐饮酒后身亡,同桌3人被判赔偿近20万元
  • 见微知沪|科学既要勇攀高峰,又要放低身段
  • 在美国,为什么夏季出生的孩子更容易得流感?
  • 技术派|威胁F-35、击落“死神”,胡塞武装防空战力如何?
  • 科普|认识谵妄:它有哪些表现?患者怎样走出“迷雾”?