首页  编辑  

Springboot+JPA+Hibernate打印完整SQL语句的方法

Tags: /Java/   Date Created:
Intercept SQL Logging with P6Spy | Baeldung

Springboot,JPA,Hibernate中,可以通过下面的方法来输出SQL语句:
spring.jpa.show-sql=true
# 格式化SQL
spring.jpa.properties.hibernate.format_sql=true
但是上面的方法,输出的是参数变成了问好?的语句,不能直接复制运行,如果要显示参数处理后的可以复制直接运行的SQL语句,可以用下面的方法:
在pom.xml中,引入依赖:
<dependency>
    <groupId>com.github.gavlyukovskiy</groupId>
    <artifactId>p6spy-spring-boot-starter</artifactId>
    <version>1.9.0</version>
 </dependency>
然后配置spy.properties即可:
appender=com.p6spy.engine.spy.appender.FileLogger
logfile=database.log
append=true
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime)|%(executionTime)|%(category)|%(sqlSingleLine)