hibernate打印的sql默認是帶佔位符的,即使添加屬性後可以展示參數,但是很不直觀,不能copy出來直接用,需要一個一個拼接好。
p6spy就可以做到
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.7.0</version>
</dependency>
新建spy.properties,放到resources目錄下
driverlist=com.mysql.jdbc.Driver
module.log=com.p6spy.engine.logging.P6LogFactory
appender=com.p6spy.engine.spy.appender.Slf4JLogger
#自定義日誌格式
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
#格式:%(currentTime)|%(executionTime)|%(category)|connection%(connectionId)|%(sqlSingleLine)
customLogMessageFormat=%(category)|conn%(connectionId)|%(sqlSingleLine)
databaseDialectDateFormat=yyyy-MM-dd HH:mm:ss
databaseDialectTimestampFormat=yyyy-MM-dd HH:mm:ss
dateformat=yyyy-MM-dd HH:mm:ss
數據庫連接池
driverClass=com.p6spy.engine.spy.P6SpyDriver
jdbcUrl=jdbc:p6spy:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8
效果
2019-09-26 17:04:26.042 [http-apr-8080-exec-6] INFO
p6spy - statement|conn2| SELECT name,age,id FROM Xxx WHERE id=111