欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot实现执行sql语句打印到控制台

程序员文章站 2022-06-27 14:46:59
springboot 执行sql语句打印到控制台1.简介每当写完持久化语句时肯定免不了要查漏补缺一波。这里就可以将执行的sql打印到控制台来检查sql语句哪里出了问题。2.配置配置非常简单,只需要在配...

springboot 执行sql语句打印到控制台

1.简介

每当写完持久化语句时肯定免不了要查漏补缺一波。这里就可以将执行的sql打印到控制台来检查sql语句哪里出了问题。

2.配置

配置非常简单,只需要在配置文件中设置下mapper日志级别就可以了

3.代码

application-test.properties
#logging.level.mapper的路径=异常级别
logging.level.com.shuhe360.auth.auth_main_car_api.mapper.carconsumerecordmapper=debug

4.jpa

如果操作数据库用的jpa也可以来手动开启打印语句到控制台的

5.jpa开启

# jpa 相关配置
spring.jpa.database-platform=org.hibernate.dialect.mysql5innodbdialect
#这里开启sql打印
spring.jpa.show-sql=false
#程序启动时删除并创建实体类对应的那个表(危险)
spring.jpa.hibernate.ddl-auto=none

springboot 打印sql 语句

在配置文件中 application.yml 配置如下其一即可

方式一:

logging:
    level:
      com.xxx.com.dao.mapper: debug //包路径为mapper文件包路径

打印出来的形式如下:

2019-01-24 08:02:14.245 [http-nio-8060-exec-2] debug c.s.a.m.m.u.getusernameexistset 159 - ==> preparing: select username from user_info where username in ( ? , ? , ? )
2019-01-24 08:02:14.245 [http-nio-8060-exec-2] debug c.s.a.m.m.u.getusernameexistset 159 - ==> parameters: nike16(string), nike14(string), nike15(string)
2019-01-24 08:02:14.307 [http-nio-8060-exec-2] debug c.s.a.m.m.u.getusernameexistset 159 - <== total: 0
2019-01-24 08:02:14.323 [http-nio-8060-exec-2] debug c.s.a.m.mapper.usermapper.savebatch 159 - ==> preparing: insert into user_info ( username, password, email, telphone, birthday, createtime, updatetime ) values ( ?, ?, ?, ?, ?, ?, ? ) , ( ?, ?, ?, ?, ?, ?, ? ) , ( ?, ?, ?, ?, ?, ?, ? )
2019-01-24 08:02:14.323 [http-nio-8060-exec-2] debug c.s.a.m.mapper.usermapper.savebatch 159 - ==> parameters: nike14(string), 4f757a334d69b32b586f3694fbaaa9a9869aee184f98e009b6e02b170f92eb9f(string), hgaha@qq.com(string), null, 2018-03-02 02:01:02.0(timestamp), 2019-01-24 08:02:14.307(timestamp), 2019-01-24 08:02:14.307(timestamp), nike15(string), 18a1c9f3e7a69e3f72ab5d80caea96e5c90f5fada8f9a7e92238dc4242ba03f8(string), hgaha@qq.com(string), null, 2018-03-02 02:01:02.0(timestamp), 2019-01-24 08:02:14.307(timestamp), 2019-01-24 08:02:14.307(timestamp), nike16(string), 5912bd4ff3ae134b15347610b64d9f352dd3c89dd2fb5c495cf4699683b33271(string), hgaha@qq.com(string), null, 2018-03-02 02:01:02.0(timestamp), 2019-01-24 08:02:14.307(timestamp), 2019-01-24 08:02:14.307(timestamp)
2019-01-24 08:02:14.338 [http-nio-8060-exec-2] debug c.s.a.m.mapper.usermapper.savebatch 159 - <== updates: 3

方式二:

mybatis
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.stdoutimpl 

打印出来的形式如下

creating a new sqlsession
registering transaction synchronization for sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2e943ddb]
jdbc connection [hikariproxyconnection@898692052 wrapping com.mysql.jdbc.jdbc4connection@6a0c5a04] will be managed by spring
==> preparing: delete from user_info where uid in ( ? , ? , ? )
==> parameters: 44(long), 45(long), 46(long)
<== updates: 0
releasing transactional sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2e943ddb]
transaction synchronization committing sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2e943ddb]
transaction synchronization deregistering sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2e943ddb]
transaction synchronization closing sqlsession [org.apache.ibatis.session.defaults.defaultsqlsession@2e943ddb]

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。