Show Hibernate/JPA SQL Statements from Spring Boot – 从Spring Boot显示Hibernate/JPA的SQL语句

最后修改: 2018年 4月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring JDBC and JPA provide abstractions over native JDBC APIs, allowing developers to do away with native SQL queries. However, we often need to see those auto-generated SQL queries and the order in which they were executed for debugging purposes.

Spring JDBCJPA提供了对本地JDBC API的抽象,使开发人员可以不使用本地SQL查询。然而,我们经常需要看到那些自动生成的SQL查询以及它们的执行顺序,以便进行调试。

In this quick tutorial, we’re going to look at different ways of logging these SQL queries in Spring Boot.

在这个快速教程中,我们将看看在Spring Boot中记录这些SQL查询的不同方法。

2. Logging JPA Queries

2.记录JPA查询

2.1. To Standard Output

2.1.至标准输出

The simplest way to dump the queries to standard out is to add the following to application.properties:

最简单的方法是在application.properties中添加以下内容,以将查询转储为标准输出。

spring.jpa.show-sql=true

To beautify or pretty print the SQL, we can add:

为了美化或漂亮地打印SQL,我们可以添加。

spring.jpa.properties.hibernate.format_sql=true

While this is extremely simple, it’s not recommended, as it directly unloads everything to standard output without any optimizations of a logging framework.

虽然这样做非常简单,但不建议这样做,因为它直接把所有东西都卸载到标准输出,没有任何日志框架的优化。

Moreover, it doesn’t log the parameters of prepared statements.

此外,它并不记录准备好的语句的参数。

2.2. Via Loggers

2.2.通过记录仪

Now let’s see how we can log the SQL statements by configuring loggers in the properties file:

现在让我们看看如何通过在属性文件中配置日志器来记录SQL语句。

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

The first line logs the SQL queries, and the second statement logs the prepared statement parameters.

第一行记录了SQL查询,第二条语句记录了准备好的语句参数。

The pretty print property will work in this configuration as well.

漂亮的打印属性在这种配置下也会起作用。

By setting these properties, logs will be sent to the configured appender. By default, Spring Boot uses logback with a standard out appender.

通过设置这些属性,日志将被发送到配置的appender。默认情况下,Spring Boot使用logback与标准out appender。

3. Logging JdbcTemplate Queries

3.记录JdbcTemplate的查询

To configure statement logging when using JdbcTemplate, we need the following properties:

要在使用JdbcTemplate时配置语句日志,我们需要以下属性。

logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE

Similar to the JPA logging configuration, the first line is for logging statements and the second is to log parameters of prepared statements.

类似于JPA的日志配置,第一行是记录语句,第二行是记录准备语句的参数。

4. How Does It Work?

4.它是如何工作的?

The Spring/Hibernate classes, which generate SQL statements and set the parameters, already contain the code for logging them.

生成SQL语句和设置参数的Spring/Hibernate类,已经包含了记录这些语句的代码。

However, the level of those log statements is set to DEBUG and TRACE respectively, which is lower than the default level in Spring Boot — INFO.

然而,这些日志语句的级别分别被设置为DEBUGTRACE,这比Spring Boot的默认级别–INFO低。

By adding these properties, we are just setting those loggers to the required level.

通过添加这些属性,我们只是将这些记录器设置为所需的级别。

5. Conclusion

5.结论

In this short article, we’ve looked at the ways to log SQL queries in Spring Boot.

在这篇短文中,我们已经了解了在Spring Boot中记录SQL查询的方法。

If we choose to configure multiple appenders, we can also separate SQL statements and other log statements into different log files to keep things clean.

如果我们选择配置多个应用程序,我们还可以将SQL语句和其他日志语句分离到不同的日志文件中,以保持干净。