
Spring Boot 3 去掉一些旧的 Hibernate 配置属性,所以在 Spring Boot 2.x 使用的参数绑定日志配置可能不再有效。本文将介绍如何使用它 Spring Boot 3 为了显示,正确配置日志 JPA 查询的 SQL 参数绑定信息。
配置日志级别要启用 SQL 需要修改参数绑定的日志记录 application.properties 或 application.yml 设置以下日志级别:
logging.level.org.hibernate.orm.jdbc.bind=trace logging.level.org.hibernate.type=trace logging.level.org.hibernate.stat=debug
或者,使用 YAML 格式:
logging:
level:
org:
hibernate:
orm:
jdbc:
bind: trace
type: trace
stat: debug
这些配置的作用如下:
- logging.level.org.hibernate.orm.jdbc.bind=trace: 启用 org.hibernate.orm.jdbc.bind 包的 trace 等级日志。这个包负责处理。 JDBC 参数绑定,trace 层次将输出详细的参数绑定信息。
- logging.level.org.hibernate.type=trace: 启用 org.hibernate.type 包的 trace 等级日志。这个包负责处理。 Hibernate 类型转换,trace 层次将输出参数类型信息。
- logging.level.org.hibernate.stat=debug: 启用 org.hibernate.stat 包的 debug 等级日志。这个包负责 Hibernate 统计信息,debug 水平可以输出一些与性能相关的调试信息,有助于优化查询。
假设我们有一个简单的假设 JPA 实体 User:
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int age;
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
以及对应的 JPA Repository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
在 Spring Boot 我们可以在应用中使用它 UserRepository 例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Create a user
User user = new User();
user.setName("John Doe");
user.setAge(30);
userRepository.save(user);
// Find all users
userRepository.findAll();
}
}
在配置了上述日志级别后,在控制台中可以看到以下类似的日志输出:
2023-10-27 10:00:00.000 TRACE [main] org.hibernate.orm.jdbc.bind - binding parameter [1] as [VARCHAR] - [John Doe] 2023-10-27 10:00:00.000 TRACE [main] org.hibernate.orm.jdbc.bind - binding parameter [2] as [INTEGER] - [30] ...
这些日志信息清楚地显示出来 SQL 语句中各参数的绑定值有助于调试 SQL 查询。
注意事项- trace 级别的日志输出非常详细,可能会产生大量的日志信息,建议只在开发和调试阶段使用。在生产环境中,可以考虑使用 debug 或 info 为了减少日志的数量,等级。
- 确保你的日志框架(例如 Logback 或 Log4j2已正确配置,以便输出 trace 等级日志。
- 若仍无法查看参数绑定信息,请检查是否已覆盖 Hibernate 例如,自定义的默认配置 DataSource 或 EntityManagerFactory。
通过正确的配置 Hibernate 日志级别,可以在 Spring Boot 3 轻松记录项目 JPA 查询中的 SQL 参数绑定信息。这对调试和优化数据库操作非常有帮助。请记住,trace 级别的日志输出非常详细,应仔细使用,并根据实际情况调整日志级别。
以上就是使用 Spring Boot 3 记录 JPA 查询中的 SQL 详情请关注图灵教育的其他相关文章!