The springboot implementation executes the sql statement and prints it to the console

  • 2021-09-11 20:14:40
  • OfStack

springboot Execute sql Statement Print to Console

1. Introduction

Whenever you finish writing a persistence statement, it is inevitable to check for leaks and fill in one wave. Here you can print the executed sql to the console to check what went wrong with the sql statement.

2. Configuration

Configuration is very simple, just set the mapper log level in the configuration file

3. Code


application-test.properties
#logging.level.mapper Path of = Exception level 
logging.level.com.shuhe360.auth.auth_main_car_api.mapper.CarConsumeRecordMapper=DEBUG

4.jpa

If you use jpa to operate the database, you can also manually open the print statement to the console

5. jpa on


# JPA  Related configuration 
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# Open here sql Print 
spring.jpa.show-sql=false
# Delete and create the table corresponding to the entity class when the program starts (dangerous) 
spring.jpa.hibernate.ddl-auto=none

springboot Print sql Statement

In the configuration file, application. yml can be configured as follows: 1

Mode 1:


logging:
    level:
      com.xxx.com.dao.mapper: DEBUG // The package path is mapper Package path 

The printed form is as follows:

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

Mode 2:


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

The printed form is as follows

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]


Related articles: