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

网友投稿 283 2023-01-09

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

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(Strihttp://ng), nike14(String), nike15(String)

2019-01-http://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]

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java并发编程之同步容器与并发容器详解
下一篇:京广快递物流查询单号(京广快递物流查询单号查询官网)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~