问题背景

通常我们开发的时候,需要联合控制台和Navicat/PLSQL等工具进行语句的拼接检查,如果只是输出了一堆???,那么将极大降低我们的效率。因此我们需要输出完整的SQL语句以便调试。

Update

解决方案(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]

解决方案(手写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);
}
}

解决方案(LOG-DEBUG模式)

# 在application.yml 中增加配置,指定 mapper 文件所在的包,进入DEBUG模式
logging:
level:
com.baomidou.example.mapper: debug

官方解决方案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

转载 mybatis-plus配置控制台打印完整带参数SQL语句的更多相关文章

  1. .net带参数SQL语句的完整定义

    首先是在DAL数据访问层中的代码://数据更新的方法public static int shuxing_update(s_passnature model) { string sql = " ...

  2. django2.1---终端打印orm转义的sql语句

    print(connection.queries)可以打印转义后的sql语句 例子: from django.http import JsonResponse,HttpResponse from .m ...

  3. 获取SQLServer的最完整数据字典的SQL语句

    原文:获取SQLServer的最完整数据字典的SQL语句 原创于2008年06月18日,2009年10月18日迁移至此. 获取SQLServer 的最完整数据字典的SQL 语句   其实网上已经流传了 ...

  4. 【转】 mybatis如何在控制台打印执行的sql语句

    <strong>######################################################################### #Root Logger ...

  5. mybatis和ibatis控制台打印sql语句方法

    #将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句 log4j.rootLogger=debug,stdout,logfile### 把日志信息输出到控制 ...

  6. 打印Ibatis最终的SQL语句

    在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改.如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理 ...

  7. ssm整合后打印日志查看执行sql语句

    mybatis.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configura ...

  8. Mybatis学习笔记(四) 之动态SQL语句

    动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: 1. if 语句 (简单的条件 ...

  9. laravel如何打印orm封装的sql语句

    $query = CdbForumSellthreadSearch::where($params)->orderBy("$orderby", "$ascDesc&q ...

  10. mybatis - 基于拦截器修改执行中的SQL语句

    拦截器介绍 mybatis提供了@Intercepts注解允许开发者对mybatis的执行器Executor进行拦截. Executor接口方法主要有update.query.commit.rollb ...

随机推荐

  1. 墨天轮访谈 | 华为云温云博:从客户视角出发,GaussDB(for Redis)究竟“香”在哪里?

    分享嘉宾:温云博 华为云数据库NoSQL团队研发工程师 整理:墨天轮社区 导读 GaussDB(for Redis)采用云原生分布式架构,完全兼容Redis协议,支持丰富数据类型. 提供数据实时持久化 ...

  2. PHP实现断点续传

    解释 业务上要求对资源文件进行加密,遂实现通过php接口调用,修改header头,传输流的方式. 测试中,在苹果手机上,如果文件过大(大概10M以上),会主动调用多次接口.此时如果不使用断点续传的方式 ...

  3. 【填算符】(log 值域的做法)

    比赛在这里呢 填算符 下发题解说的神马东西,赛时根本想不到 讲一个赛时想得到的 \(O(n\log 值域)\) 的思路,很好理解 我们处理出二进制下每一位上的 1 的最后一次出现的位置,将第 \(i\ ...

  4. 解决Python的pip问题:WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None))

    相关: pip安装第三方库报错Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) 国内镜像源下 ...

  5. Lattice ICE40LP8K开发

    一.开发工具: ICEcube2,界面非常原始,只有PLL IP核添加功能,其他IP核貌似只能使用primitive替换. 不支持时序分析.在线仿真等功能. 二.原语使用 全局布线资源 在 iCE40 ...

  6. 基于Java+SpringBoot+Mysql实现的快递柜寄取快递系统功能实现一

    一.前言介绍: 1.1 项目摘要 随着电子商务的迅猛发展和城市化进程的加快,快递业务量呈现出爆炸式增长的趋势.传统的快递寄取方式,如人工配送和定点领取,已经无法满足现代社会的快速.便捷需求.这些问题不 ...

  7. 12.Kubernetes集群安全机制

    Kubernetes集群安全机制 概述 当我们访问K8S集群时,需要经过三个步骤完成具体操作 认证 鉴权[授权] 准入控制 进行访问的时候,都需要经过 apiserver, apiserver做统一协 ...

  8. CF1515F Phoenix and Earthquake

    CF1515F Phoenix and Earthquake 证明题. 思路 考虑不合法的情况,如果 \(\sum a_i < (n-1)\times x\),肯定是不合法的. 再考虑对于一个可 ...

  9. CF207C3 Game with Two Trees

    CF207C3 Game with Two Trees 妙到家的树上字符串问题. 约定 树 \(1\):\(t_1\). 树 \(2\):\(t_2\). \(S_{1/2}(i,l)\) 为树 \( ...

  10. typeScript 基础类型 (三)

    typeScript 的基础类型包含 Boolean.Number.String.null.undefined 以及 ES6 的  Symbol 和 ES10 的 BigInt. 下面介绍每种类型的使 ...