Spring整合Mybatis打印SQL语句

导读:本篇文章讲解 Spring整合Mybatis打印SQL语句,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

在本地开发调试时,某些场景下想看看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]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100311.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!