Koril
V2EX  ›  问与答

SpringBoot + JPA 的日志打印问题

  •  
  •   Koril · Aug 12, 2023 · 1492 views
    This topic created in 1020 days ago, the information mentioned may be changed or developed.

    一开始是使用以下配置,打印 SQL 语句:

    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    

    发现无法打印查询的参数,遂按照网上的一些教程改成了以下配置:

    spring.jpa.properties.hibernate.format_sql=true
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
    

    但是我发现这样配置以后,查询参数虽然打印出来了,但是缺少 limit 和 offset 的参数值,请问大佬们该如何解决这个问题?

    1 replies    2023-08-12 15:40:37 +08:00
    BiChengfei
        1
    BiChengfei  
       Aug 12, 2023
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>3.1.0</version>
    </dependency>

    `application.yml`配置:
    ```
    logging:
    level:
    root: info
    org:
    hibernate:
    orm:
    jdbc:
    bind: trace
    ```
    连接 MySQL 8.0 ,打印日志如下:
    ```
    Hibernate: select e1_0.id,e1_0.name,e1_0.tenantId,e1_0.timeCreated,e1_0.timeLastUpdated from t_main_ent e1_0 order by e1_0.id limit ?,?
    2023-08-12T15:32:00.610+08:00 TRACE 2644 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind : binding parameter [1] as [INTEGER] - [0]
    2023-08-12T15:32:00.610+08:00 TRACE 2644 --- [nio-8080-exec-1] org.hibernate.orm.jdbc.bind : binding parameter [2] as [INTEGER] - [234]
    Hibernate: select e1_0.id,e1_0.name,e1_0.tenantId,e1_0.timeCreated,e1_0.timeLastUpdated from t_main_ent e1_0 where e1_0.id=?
    2023-08-12T15:34:51.807+08:00 TRACE 2644 --- [nio-8080-exec-5] org.hibernate.orm.jdbc.bind : binding parameter [1] as [BIGINT] - [1]
    ```
    个人经验之谈:在不知道是哪个类输入的日志之前,就把整个项目的日志级别( logging.level.root )改为最小粒度,trace 一般就可以,然后从日志里面看对应的 SQL 和参数是哪个类输出的,就把对应的类的日志级别设置成相应的粒度
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5291 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 07:44 · PVG 15:44 · LAX 00:44 · JFK 03:44
    ♥ Do have faith in what you're doing.