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

Springboot + Mybatis 实现sql打印

参照这个视频:https://www.bilibili.com/video/BV1MS411N7mn/?vd_source=90ebeef3261cec486646b6583e9f45f5
实现mybatis对外暴露的接口Interceptor
使用@Intercepts接口,这里的写法参照mybatis-plus中的拦截器写法
在这里插入图片描述

@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}),@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
})
public class MybatisSqlPrint implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {// 计算这一次SQL执行钱后的时间,统计一下执行耗时long startTime = System.currentTimeMillis();Object proceed = invocation.proceed();long endTime = System.currentTimeMillis();String printSql = null;try {// 通过generateSql方法拿到最终生成的SQLprintSql = generateSql(invocation);}catch (Exception exception){System.err.println(String.format("获取sql异常:%s",exception));}finally {// 拼接日志打印过程long costTime = endTime - startTime;System.out.println(String.format("\n 执行SQL耗时:%dms \n 执行SQL:%s",costTime,printSql));}return proceed;}private static String generateSql(Invocation invocation){// 获取到BoundSql以及Configuration对象// BoundSql 对象存储了一条具体的 SQL 语句及其相关参数信息。// Configuration 对象保存了 MyBatis 框架运行时所有的配置信息MappedStatement statement = (MappedStatement) invocation.getArgs()[0];Object parameter = null;if (invocation.getArgs().length>1){parameter = invocation.getArgs()[1];}Configuration configuration = statement.getConfiguration();BoundSql boundSql = statement.getBoundSql(parameter);// 获取参数对象Object parameterObject = boundSql.getParameterObject();// 获取参数映射List<ParameterMapping> params = boundSql.getParameterMappings();// 获取到执行的SQLString sql = boundSql.getSql();// SQL中多个空格使用一个空格代替sql = sql.replaceAll("[\\s]+", " ");if (!ObjectUtils.isEmpty(params) && !ObjectUtils.isEmpty(parameterObject)){// TypeHandlerRegistry 是 MyBatis 用来管理 TypeHandler 的注册器。TypeHandler 用于在 Java 类型和 JDBC 类型之间进行转换TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();// 如果参数对象的类型有对应的 TypeHandler,则使用 TypeHandler 进行处理if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())){sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(parameterObject)));}else {// 否则,逐个处理参数映射for (ParameterMapping param : params) {// 获取参数的属性名String propertyName = param.getProperty();MetaObject metaObject = configuration.newMetaObject(parameterObject);// 检查对象中是否存在该属性的 getter 方法,如果存在就取出来进行替换if (metaObject.hasGetter(propertyName)){Object obj = metaObject.getValue(propertyName);sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj)));// 检查 BoundSql 对象中是否存在附加参数。附加参数可能是在动态 SQL 处理中生成的,有的话就进行替换}else if (boundSql.hasAdditionalParameter(propertyName)){Object obj = boundSql.getAdditionalParameter(propertyName);sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj)));}else {// 如果都没有,说明SQL匹配不上,带上“缺失”方便找问题sql = sql.replaceFirst("\\?", "缺失");}}}}return sql;}private static String getParameterValue(Object object) {String value = "";if (object instanceof String){value = "'" + object.toString() + "'";}else if (object instanceof Date){DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);value = "'" + format.format((Date) object) + "'";} else if (!ObjectUtils.isEmpty(object)) {value = object.toString();}return value;}}

最后将拦截器添加到mybatis中

@Configuration
public class MybatisConfig {public MybatisConfig() {System.out.println("MybatisConfig loaded");}SqlSessionFactory sqlSessionFactory;@Autowiredpublic void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {this.sqlSessionFactory = sqlSessionFactory;}@PostConstructpublic void addInterceptor(){sqlSessionFactory.getConfiguration().addInterceptor(new MybatisSqlPrint());}}

实现效果
在这里插入图片描述

http://www.lryc.cn/news/382957.html

相关文章:

  • Cesium默认bing地图数据,还支持哪些地图的数据源呢?
  • 高效、智能、安全:小型机房EasyCVR+AI视频综合监控解决方案
  • 数据分析的Excel基础操作
  • 【C语言】解决C语言报错:Invalid Pointer
  • 动态图形设计:创造视觉运动的艺术
  • CSS 属性 `mix-blend-mode`
  • 三大交易所全面恢复 IPO 申请
  • VC++开发积累——vc++6.0中删除函数的方法,右键,Delete
  • HBDNY-40/1端子排电压继电器 DC110V 导轨安装 约瑟JOSEF
  • Redis-数据类型-Geospatial(地理空间索引)
  • Python联动Mysql
  • vue3-openlayers 轨迹回放(历史轨迹)(ol-animation-path实现)
  • 计算机视觉全系列实战教程 (十二):图像分割(阈值分割threshold、分水岭算法watershed的使用步骤、洪水填充floodFill算法的使用)
  • Linux的免交互
  • 查看es p12证书文件过期方法
  • 1.8 无符号大数加、减运算
  • Java常用类--包装类
  • SpringMvcの拦截器全局异常处理
  • JVM虚拟机的组成
  • 探索CSS clip-path: polygon():塑造元素的无限可能
  • 【华为OD机试B卷】单词接龙(C++/Java/Python)
  • 项目实训-vue(十七)
  • Android10 SystemUI系列 需求定制(二)隐藏状态栏通知图标,锁屏通知,可定制包名,渠道等
  • Linux:RAID磁盘阵列
  • MongoDB和AI 赋能行业应用:零售
  • MQ~消息队列能力、AMQP协议、现有选择(Kafka、RabbitMQ、RocketMQ 、Pulsar)
  • 开源网安参与编制的《代码大模型安全风险防范能力要求及评估方法》正式发布
  • 【树状数组 队列】1505. 最多 K 次交换相邻数位后得到的最小整数
  • 【附精彩文章合辑】当谈到程序的“通用性”与“过度设计”的困境时,我们可以通过一些具体的例子来更直观地阐述这些解决方案
  • Word中删除空白页