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(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), [email protected](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), [email protected](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), [email protected](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]

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: