웹 개발

Spring Boot SQL 보기 옵션 총 정리

노루아부지 2021. 8. 8. 22:14
  1. SQL 보기 옵션
    - Hibernate가 DB에 보내는 모든 쿼리를 보여줍니다.
    1. application.properties인 경우
      spring.jpa.properties.hibernate.show_sql=true​
    2. application.yml인 경우
      spring:
        jpa:
          properties:
            hibernate:
              show_sql: true​
    3. 추가된 로그
      Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ where user0_.name=?​
  2. sql 보기 좋게 변경
    1. properties인 경우
      spring.jpa.properties.hibernate.format_sql=true​
    2. yml인 경우
      spring:
        jpa:
          properties:
            hibernate:
              format_sql: true​
    3. 바뀐 로그
      Hibernate:
        select
          user0_.id as id1_0_,
          user0_.age as age2_0_,
          user0_.name as name3_0_
        from
          user user0_
        where
          user0_.name=?​
  3. 추가 정보 표시
    1. properties인 경우
      spring.jpa.properties.hibernate.use_sql_comments=true​​
    2. yml인 경우
      spring:
        jpa:
          properties:
            hibernate:
              use_sql_comments: true​
    3. 바뀐 로그
      /* */ 안에 주석이 추가된 것을 알 수 있습니다.
      /*  select
            generatedAlias0
          from
              User as generatedAlias0
          where
              generatedAlias0.name=:param0 */ select
                  user0_.id as id1_0_,
                  user0_.age as age2_0_,
                  user0_.name as name3_0_
              from
                  user user0_
              where
                  user0_.name=?​
      */​
  4. ?(parameter)에 어떤 값이 들어갔는지 확인하기
    1. properties인 경우
      logging.level.org.hibernate.type.descriptor.sql=trace​
    2. yml인 경우
      logging:
        level:
          org:
            hibernate:
              type:
                descriptor:
                  sql: trace​
    3. 주의할 점
      - 이 옵션을 활성화하면 parameter 뿐만 아니라 query의 결과도 모두 표시되기 때문에 어마어마한 로그가 표시될 수 있습니다.

 

[참고 사이트]

https://stackoverflow.com/questions/30118683/how-to-log-sql-statements-in-spring-boot

 

How to log SQL statements in Spring Boot?

I want to log SQL statements in a file. I have the following properties in application.properties spring.datasource.url=... spring.datasource.username=user spring.datasource.password=1234 spring.

stackoverflow.com

 

728x90
loading