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

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.dtcms.com/a/201073.html

相关文章:

  • 深入探讨死区生成:原理、实现与应用
  • MIMO 检测(6)--最大似然检测(1)
  • 基于SpringBoot的小型民营加油站管理系统
  • 2025年—ComfyUI_关于ComfyUI的零碎小知识
  • unity UGUI虚线框shader
  • C++--综合应用-演讲比赛项目
  • 10、底层注解-@Conditional条件装配
  • python可视化:公积金与商业贷款利率历年趋势1
  • 客户体验数据使用的三种视角——旅程视角
  • 升级mysql (rpm安装)
  • 【java第14集】java访问修饰符详解
  • HashMap 的特点及应用场景
  • 洛谷B3876—— [信息与未来 2015] 中间值
  • Java知识点-Stream流
  • UE5在Blueprint中判断不同平台
  • MySQL如何快速删除数据库中所有表数据
  • PyMOL结构对齐方式
  • 【深度学习新浪潮】什么是多模态大模型?
  • 列表 模版题单 12
  • Windows 如何安装CUDA
  • LeetCode 字符串类题目解析与 Java 实现指南(深度优化版)
  • leetcode 每日一题 1931. 用三种不同颜色为网格涂色
  • gtest 库的安装和使用
  • python打卡第30天
  • dbhub MCP服务搭建
  • cursor+MCP系列:12306-MCP车票查询工具
  • Logrotate:配置日志轮转、高效管理Linux日志文件
  • 秒删node_modules 极速删除 (rimraf工具)
  • 非金属材料的分类及应用
  • C++寻位映射的奇幻密码:哈希