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

mybtais plus使用拦截器打印完整SQL语句

1、pom文件

        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.5</version></dependency><dependency><groupId>com.github.yulichang</groupId><artifactId>mybatis-plus-join-boot-starter</artifactId><version>1.5.3</version></dependency>

2、拦截器

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.stereotype.Component;import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;/*** SQL打印拦截器,用于打印完整的SQL语句和参数值*/@Slf4j
@Component
public class SqlPrintInnerInterceptor implements InnerInterceptor {@Overridepublic void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {// 获取SQL语句String sql = showSql(ms.getConfiguration(), boundSql);log.info("===========================================");log.info("SQL: {}", sql);log.info("===========================================");log.info("Mapper: {}",ms.getId());}@Overridepublic void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {BoundSql boundSql = ms.getBoundSql(parameter);// 获取SQL语句String sql = showSql(ms.getConfiguration(), boundSql);log.info("===========================================");log.info("\nSQL: {}", sql);log.info("===========================================");log.info("Mapper: {}",ms.getId());}public static String showSql(Configuration configuration, BoundSql boundSql) {Object parameterObject = boundSql.getParameterObject();List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();String sql = boundSql.getSql().replaceAll("[\\s]+", " ");if (parameterMappings.size() > 0 && parameterObject != null) {TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));} else {MetaObject metaObject = configuration.newMetaObject(parameterObject);for (ParameterMapping parameterMapping : parameterMappings) {String propertyName = parameterMapping.getProperty();if (metaObject.hasGetter(propertyName)) {Object obj = metaObject.getValue(propertyName);sql = sql.replaceFirst("\\?", getParameterValue(obj));} else if (boundSql.hasAdditionalParameter(propertyName)) {Object obj = boundSql.getAdditionalParameter(propertyName);sql = sql.replaceFirst("\\?", getParameterValue(obj));}}}}return sql;}private static String getParameterValue(Object obj) {String value = null;if (obj instanceof String) {value = "'" + obj.toString() + "'";} else if (obj instanceof Date) {DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);value = "'" + formatter.format(new Date()) + "'";} else {if (obj != null) {value = obj.toString();} else {value = "";}}return value;}}

3、mybatis plus插件添加拦截器

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** mybatis-plus配置** @author Mark sunlightcs@gmail.com*/
@Configuration
public class MybatisPlusConfig {/*** 添加分页插件和自定义SQL打印插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 添加分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 添加自定义SQL打印插件interceptor.addInnerInterceptor(new SqlPrintInnerInterceptor());return interceptor;}}

http://www.xdnf.cn/news/7469.html

相关文章:

  • Pyro:基于PyTorch的概率编程框架
  • 代码审查服务费用受哪些因素影响?如何确定合理报价?
  • 《Opensearch-SQL》论文精读:2025年在BIRD的SOTA方法(Text-to-SQL任务)
  • reshape/view/permute的原理
  • 7-2 银行业务队列简单模拟
  • 【PhysUnits】4.5 负数类型(Neg<P>)算术运算(negative.rs)
  • Node.js 实战八:服务部署方案对比与实践
  • 应对WEEE 2025:猎板PCB的区块链追溯与高温基材创新
  • 牛客网 NC274692 题解:素世喝茶
  • 低空经济的法律挑战与合规实践
  • uv 包管理工具使用教程
  • pkg-config 是什么,如何工作的
  • 深入解析`lsof`命令:查看系统中打开文件与进程信息
  • 【Nuxt3】安装 Naive UI 按需自动引入组件
  • ThreadLocal 源码深度解析
  • Linux基础第四天
  • goldenDB创建函数索引报错问题
  • 鸿蒙 Background Tasks Kit(后台任务开发服务)
  • 北京本地 SEO 推广:从技术成本到效果转化的深度拆解
  • 从零训练一个大模型:DeepSeek 的技术路线与实践
  • 苏州SMT贴片加工服务选择指南
  • MCP详解
  • Python中的整型(int)和浮点数(float)
  • 哈希表和哈希函数
  • 养生攻略:打造活力健康日常
  • 《 二级指针:解锁指针的进阶魔法》
  • GPT/Claude3国内免费镜像站更新 亲测可用
  • 活学妙用——5W2H分析法
  • 【java第17集】java流程控制语句详解
  • 按键太频繁导致,报不应该报的错误!