当前位置: 首页 > 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;}}

相关文章:

  • 深入探讨死区生成:原理、实现与应用
  • 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
  • 英国研究:近七成年轻人认为上网有害心理健康
  • 复旦建校120周年|迎来复旦大学艺术馆开馆
  • 莱布雷希特专栏:古典乐坛边缘人
  • 李公明 | 一周画记:德里达哲学还是接地气的
  • 不赚“快钱”的佳沛:蒋时杰解密新西兰国果如何在中国“慢养”出43亿生意
  • 《缶翁的世界》首发:看吴昌硕等湖州籍书画家的影响