mybatis-plus配置控制台打印完整带参数SQL语句的实现
作者:Moshow郑锴 发布时间:2023-11-24 22:43:58
问题背景
通常我们开发的时候,需要联合控制台和Navicat/PLSQL等工具进行语句的拼接检查,如果只是输出了一堆???,那么将极大降低我们的效率。因此我们需要输出完整的SQL语句以便调试。
update 2020-July : 新增官方p6spy打印分析sql语句方案
解决方案(StdOutImpl)
请注意: 部分朋友反馈不生效,估计跟引入的包有一定关系,druid+mybatis-plus-boot-starter 就亲测有用。请检查是否有log4j相关实现类。
如果是application.yml
#by zhengkai.blog.csdn.net
#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
如果是application.properties,添加:
#mybatis-plus配置控制台打印完整带参数SQL语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种方式:
SLF4J
Apache Commons Logging
Log4j 2
Log4j
JDK logging
no logging
具体选择哪个日志实现由MyBatis的LogFactory内置日志工厂确定。它会使用最先找到的(按上文列举的顺序查找)。 如果一个都未找到,日志功能就会被禁用。
static {
tryImplementation(LogFactory::useSlf4jLogging);
tryImplementation(LogFactory::useCommonsLogging);
tryImplementation(LogFactory::useLog4J2Logging);
tryImplementation(LogFactory::useLog4JLogging);
tryImplementation(LogFactory::useJdkLogging);
tryImplementation(LogFactory::useNoLogging);
}
不少应用服务器的classpath中已经包含Commons Logging,如Tomcat和WebShpere, 所以MyBatis会把它作为具体的日志实现。
记住这点非常重要。这意味着,在诸如 WebSphere的环境中——WebSphere提供了Commons Logging的私有实现,你的Log4J配置将被忽略。
这种做法不免让人悲摧,MyBatis怎么能忽略你的配置呢?事实上,因Commons Logging已经存 在,按优先级Log4J自然就被忽略了!
控制台输出
--- [ XNIO-1 task-12] c.s.cms.controller.IndexController : username-admin-password-123456-****
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@708e9ffd] was not registered for synchronization because synchronization is not active
--- [ XNIO-1 task-12] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@62b13210] will not be managed by Spring
==> Preparing: select * from user t where t.user_code='admin' and t.password='123456'
==> Parameters:
<== Columns: user_id, user_code, create_date, modify_date, user_name, password, status, role_id, department_id, major_id, classes_id, year
<== Row: 1, admin, 2020-02-15 22:14:32, 2020-02-18 23:38:51, Moshow K ZHENG, 123456, 1, 9, 1, 13, 113, 2020
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@708e9ffd]
解决方案2(手写一个MybatisPlusOutImpl)
配置文件
mybatis-plus:
configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 改为自己写的
log-impl: com.softdev.system.config.MybatisPlusOutImpl
java类 MybatisPlusOutImpl
package com.softdev.system.config;
import org.apache.ibatis.logging.Log;
/**
* @Description MybatisPlusOutImpl,直接使用控制台输出日志
* @Author zhengkai.blog.csdn.net
**/
public class MybatisPlusOutImpl implements Log {
public MybatisPlusOutImpl(String clazz) {
System.out.println("MybatisPlusOutImpl::"+clazz);
}
public boolean isDebugEnabled() {
return true;
}
public boolean isTraceEnabled() {
return true;
}
public void error(String s, Throwable e) {
System.err.println(s);
e.printStackTrace(System.err);
}
public void error(String s) {
System.err.println("MybatisPlusOutImpl::"+s);
}
public void debug(String s) {
System.out.println("MybatisPlusOutImpl::"+s);
}
public void trace(String s) {
System.out.println("MybatisPlusOutImpl::"+s);
}
public void warn(String s) {
System.out.println("MybatisPlusOutImpl::"+s);
}
}
官方解决方案p6spy
查看p6spy最新版本 ,请注意,该方案为侵入式的JDBC级方案。
pom.xml引入
<!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
这是yaml版本,还没试过,待我实验一下.
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:h2:mem:test
...
这是官方提供的properties版本
#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
来源:https://blog.csdn.net/moshowgame/article/details/90641657
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 本文实例讲述了Android编程重写ViewGroup实现卡片布局的方法。分享给大家供大家参考,具体如下:实现效果如图:实现思路1. 重写o
- 本文实例讲述了Android Appwidget用法。分享给大家供大家参考,具体如下:App Widgets 是一个小型应用程序的View&
- 1.1、获取http请求参数是一种刚需我想有的小伙伴肯定有过获取http请求的需要,比如想前置获取参数,统计请求数据做服务的接口签名校验敏感
- 前言在Kotlin中四种可见性修饰符:private、protected、internal、public,如果没有显示指定修饰符的话,默认可
- 本文实例为大家分享了C#实现数字华容道游戏的具体代码,供大家参考,具体内容如下代码如下:using System;using System.
- 这篇文章主要介绍了Java判断主机是否能ping通代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 基础概念百度百科是这么描述归并排序的: 归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。设有数列{6,
- TCP和UDP在网络传输中非常重要,在Android开发中同样重要。首先我们来看一下什么是TCP和UDP。什么是TCP?TCP:Transm
- 1.类加载机制Java中的源码.java后缀文件会在运行前被编译成.class后缀文件,文件内的字节码的本质就是一个字节数组 ,它有特定的复
- 本文实例讲述了Android编程实现图片的浏览、缩放、拖动和自动居中效果的方法。分享给大家供大家参考,具体如下:Touch.java/**
- 1、项目启动时报错如下Description:The bean 'securityManager', defined in
- 本文实例讲述了Java数组的定义、初始化、及二维数组用法。分享给大家供大家参考,具体如下:数组的定义1.数组是有序数据的集合,数组中的每个元
- 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了。在讲解之间我们先看如下一个经典的案
- 这篇文章主要介绍了Java如何实现支付宝电脑支付基于servlet版本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 本文实例为大家分享了Unity封装延时调用定时器的具体代码,供大家参考,具体内容如下封装一个延时调用定时器类using System.Col
- 一. 递归1. 简介所谓的递归,其实是一种解决问题的方式。就是在解决具有既定规律的问题时,在方法内部调用方法自身的一种编程方式。 即方法在运
- 一、Media FrameWork背景Media Framework (媒体函数库):此函数库让Android 可以播放与录制许多常见的音频
- 今天给大家带来的是一块用WPF 实现魔方的小游戏,先看一下效果图 代码如下,先写一个类,用来判断是否可以移动using System;usi
- 目录前言应用定义基本Enum特性Enum的静态导入Enum中添加新方法Switch语句中的EnumEnum的继承EnumSet的使用Enum
- gRPCgRPC是由 google开发的一个高性能、通用的开源RPC框架,主要面向移动应用开发且基于HTTP/2协议标准而设计,同时支持大多