springboot-How to print JPA and jdbcTemplate SQL logs in SpringBoot apps?

1. The purpose of this post

This demo show how to print SQL logs of your dao when using springboot apps.

2. Environments

  • SpringBoot 1.x and 2.x
  • Java 1.8+

3. JPA solution

Just add this to your application.properties

spring.jpa.show-sql=true

Then you would get this:

Hibernate: select student0_.id as id1_0_, student0_.branch as branch2_0_, student0_.email as email3_0_, student0_.name as name4_0_, student0_.percentage as percenta5_0_, student0_.phone as phone6_0_ from tbl_student student0_ limit ?

You can find detail document of this settings in Data Access of SpringBoot Reference.

4. JdbcTemplate solution

Just add this to your application.properties

logging.level.=ERROR
logging.level.org.springframework.jdbc.core = TRACE

Then you would get this:

2019-05-11 12:11:10.708 DEBUG 8852 --- [nio-8080-exec-1] o.s.jdbc.core.JdbcTemplate               : Executing SQL query [select * from tbl_city]

-->