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

SpringBoot Mybatis-Plus 日志带参数

SpringBoot Mybatis-Plus 日志带参数

  • 1 实现代码
  • 2 测试结果

在Spring Boot中,MyBatis插件机制通过拦截器(Interceptor)来实现。拦截器允许开发人员在执行SQL语句的各个阶段(如SQL语句创建、参数处理、结果映射等)插入自定义逻辑。MyBatis的拦截器主要用于增强MyBatis的功能,如日志记录、性能监控、实现分页、数据权限控制、SQL日志、动态SQL生成等,在SQL执行之前后,提供了四个拦截点,在方法执行前后进行处理,支持不同场景的功能扩展。

作用
Executor用于执行增/删/改/查等数据库操作。
ParameterHandler用于处理 SQL 参数。
ResultSetHandler用于处理结果集。
StatementHandler用于处理 SQL 语句的创建和参数化。

@Signature参数说明

参数参数说明
type就是指定拦截器类型(Executor, ParameterHandler, StatementHandler, ResultSetHandler)。
method是拦截器类型中的方法,不是自己写的方法(update, query, prepare …)。
args是method中方法的入参。

Executor接口中常见的方法

方法实现原理
update执行插入、更新和删除操作。
query执行查询操作,返回结果集。
flushStatements刷新批量执行的 SQL 语句。
commit提交事务。
rollback回滚事务。
getTransaction获取当前事务对象。
close关闭执行器。
isClosed检查执行器是否已关闭。
clearLocalCache清空本地缓存。

Mybatis拦截器的实现原理

方法实现原理
注册拦截器拦截器需要在 MyBatis 配置文件中注册,通常是在 mybatis-config.xml 中配置,也可以通过 Java 配置类进行注册。
实现 Interceptor 接口自定义拦截器需要实现 MyBatis 的 Interceptor 接口,并重写 intercept 方法。这个方法会接收一个 Invocation 对象,表示被拦截的方法调用。
调用链MyBatis 在执行 SQL 操作时,会按照拦截器的配置顺序,依次调用每个拦截器的 intercept 方法。拦截器可以选择在方法执行前后进行处理,或者直接修改方法参数和返回值。
@Intercepts({@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})
})
public class MyInterceptor implements Interceptor {...
}

1 实现代码

import java.text.DateFormat;
import lombok.extern.slf4j.Slf4j;import cn.hutool.core.collection.CollUtil;import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.regex.Matcher;import java.sql.Connection;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
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.session.Configuration;
import org.apache.ibatis.type.TypeHandlerRegistry;@Slf4j
@Component
@ConditionalOnProperty(prefix = "mybatis-plus", name = "customer-log", havingValue = "true")
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class MybatisLogger implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler handler = (StatementHandler) invocation.getTarget();MetaObject object = MetaObject.forObject(handler,SystemMetaObject.DEFAULT_OBJECT_FACTORY,SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory());MappedStatement statement = (MappedStatement) object.getValue("delegate.mappedStatement");log.info("SQL执行类: {}", statement.getId());log.info("SQL执行类型: {}", statement.getSqlCommandType().toString());BoundSql bound = handler.getBoundSql();Configuration configuration = statement.getConfiguration();String sql = getFullSql(configuration, bound);log.info("SQL语句: {}", sql);long start = System.currentTimeMillis();Object value = invocation.proceed();long end = System.currentTimeMillis();log.info("SQL耗时: {}", (end - start));return value;}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {// 可以通过MyBatis配置文件或注解传递属性}public String getFullSql(Configuration conf, BoundSql bound) {Object object = bound.getParameterObject();List<ParameterMapping> list = bound.getParameterMappings();String sql = bound.getSql().replaceAll("[\\s]+", " ").toLowerCase(Locale.ROOT);if (CollUtil.isNotEmpty(list) && object != null) {TypeHandlerRegistry type = conf.getTypeHandlerRegistry();if (type.hasTypeHandler(object.getClass())) {sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParaValue(object)));} else {MetaObject meta = conf.newMetaObject(object);for (ParameterMapping parameterMapping : list) {String name = parameterMapping.getProperty();if (meta.hasGetter(name)) {Object obj = meta.getValue(name);sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParaValue(obj)));} else if (bound.hasAdditionalParameter(name)) {Object obj = bound.getAdditionalParameter(name);sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParaValue(obj)));} else {sql = sql.replaceFirst("\\?", "缺失");}}}}return sql;}private String getParaValue(Object obj) {if (obj instanceof String) {return "'" + obj + "'";} else if (obj instanceof Date) {DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);return "'" + formatter.format(new Date()) + "'";} else {if (obj != null) {return obj.toString();} else {return "";}}}}

2 测试结果

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@264df4aa]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d91483c] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@44d84b5a] will not be managed by Spring
2024-07-05 16:32:27.660  INFO 3264 --- [nio-8221-exec-1] com.xu.hander.MybatisLogger   : SQL执行类: com.xu.view.mapper.StudentMapper.selectOne
2024-07-05 16:32:27.660  INFO 3264 --- [nio-8221-exec-1] com.xu.hander.MybatisLogger   : SQL执行类型: SELECT
2024-07-05 16:32:27.660  INFO 3264 --- [nio-8221-exec-1] com.xu.hander.MybatisLogger   : SQL语句: select id,create_by,create_time,create_hour,create_date,update_by,update_time,tenant_id,user_id,cus_id,page,number,is_share,time,event from student where (cus_id = 3 and page = 3 and event = 5) order by create_time desc limit 1
2024-07-05 16:32:27.663  INFO 3264 --- [nio-8221-exec-1] com.xu.hander.MybatisLogger   : SQL耗时: 3
http://www.lryc.cn/news/396037.html

相关文章:

  • 【WebGIS平台】传统聚落建筑科普数字化建模平台
  • Zookeeper分布式锁原理说明【简单易理解】
  • 去除Win32 Tab Control控件每个选项卡上的深色对话框背景
  • iis部署前后端分离项目(React前端,Node.js后端)
  • 【前端项目笔记】9 数据报表
  • 等保测评推动哈尔滨数字化转型中的安全保障
  • #pragma 指令
  • 【Excel】 批量跳转图片
  • 网站更新改版了
  • 初识大模型
  • Open3D SVD算法实现对应点集配准
  • bWAPP靶场安装
  • SpringBoot + MyBatisPlus 实现多租户分库
  • 【数据挖掘】银行信用卡风险大数据分析与挖掘
  • 使用 Qt 和 ECharts 进行数据可视化
  • 【机器学习】在【Pycharm】中的实践教程:使用【逻辑回归模型】进行【乳腺癌检测】
  • 【搭建Nacos服务】centos7 docker从0搭建Nacos服务
  • 将 build.gradle 配置从 Groovy 迁移到 Kotlin
  • 5G(NR) NTN 卫星组网架构
  • WEB安全-文件上传漏洞
  • Python函数 之 函数基础
  • 昇思25天学习打卡营第11天|SSD目标检测
  • MySQL篇五:基本查询
  • FreeBSD@ThinkPad x250因电池耗尽关机后无法启动的问题存档
  • pdfplumber vs PyMuPDF:PDF文本、图像和表格识别的比较
  • 深入Django系列
  • 【Python】找Excel重复行
  • 重读AI金典算法模型-GPT系列
  • 仙人掌中的SNMP检测不到服务器
  • git只列出本地分支