Spring整合Mybatis打印SQL语句

概述

在本地开发调试时,某些场景下想看看mybatis执行的sql语句到底什么,因此我需要把sql执行语句打印出来。

spring框架中

在mybatis的配置文件中加下面配置
加入这样一句话就可以看到sql语句及其参数情况了。

<setting name="logImpl" value="STDOUT_LOGGING" />

整体配置大致如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <typeAliases>
        <package name="com.hehe.gogo.pojo"/>
    </typeAliases>
</configuration>

springboot框架中

  1. 控制台打印sql语句
  # 增加打印sql语句,一般用于本地开发测试
        mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
  1. 配置示例
#tomcat port
server.port=8080
#datasource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://xxx:3306/xxx?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
#logging
#TkMybatis
mybatis.mapper-locations=classpath*:mapper/*.xml
# sql打印
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

测试结果

JDBC Connection [HikariProxyConnection@10288247 wrapping com.mysql.cj.jdbc.ConnectionImpl@7418d76e] will not be managed by Spring
==>  Preparing: SELECT id,user_name,sex,age,address,phone FROM test_user WHERE ( ( user_name = ? ) ) 
==> Parameters: 张三(String)
<==    Columns: id, user_name, sex, age, address, phone
<==        Row: 1, 张三, null, 18, null, null
<==        Row: 1, 张三, null, 18, null, null
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@750a04ec]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融极

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值