2

My project is built with Spring boot and Mybatis, I want to see the sql query statements, what can I do?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • There are some options given [here](https://stackoverflow.com/q/27060563/217324) – Nathan Hughes May 16 '19 at 12:03
  • Another option here: https://stackoverflow.com/questions/2635058/ibatis-get-executed-sql – DwB May 16 '19 at 15:04
  • Here is another one: https://stackoverflow.com/questions/4082834/ibatis-spring-how-to-log-the-sql-that-is-executed/4110262#4110262 – DwB May 16 '19 at 15:48
  • Note: ibatis is the old name of Mybatis. – DwB May 16 '19 at 15:49
  • Possible duplicate of [Spring-boot with spring-mybatis - how to force it to logging all SQL queries](https://stackoverflow.com/questions/41001188/spring-boot-with-spring-mybatis-how-to-force-it-to-logging-all-sql-queries) – Roman-Stop RU aggression in UA May 17 '19 at 09:43

1 Answers1

4

Assuming your mappers are in a package your.pkg.mapper, adding the following line to application.properties tells MyBatis to print statements, parameters and query results.

logging.level.your.pkg.mapper=TRACE

The below is the console output of the sample project.

2019-05-17 01:01:01.631 DEBUG 76540 --- [           main] s.m.mapper.CityMapper.selectCityById     : ==>  Preparing: select id, name, state, country from city where id = ? 
2019-05-17 01:01:01.671 DEBUG 76540 --- [           main] s.m.mapper.CityMapper.selectCityById     : ==> Parameters: 1(Long)
2019-05-17 01:01:01.705 TRACE 76540 --- [           main] s.m.mapper.CityMapper.selectCityById     : <==    Columns: ID, NAME, STATE, COUNTRY
2019-05-17 01:01:01.705 TRACE 76540 --- [           main] s.m.mapper.CityMapper.selectCityById     : <==        Row: 1, San Francisco, CA, US
2019-05-17 01:01:01.711 DEBUG 76540 --- [           main] s.m.mapper.CityMapper.selectCityById     : <==      Total: 1
2019-05-17 01:01:01.724 DEBUG 76540 --- [           main] s.m.mapper.HotelMapper.selectByCityId    : ==>  Preparing: select city, name, address, zip from hotel where city = ? 
2019-05-17 01:01:01.724 DEBUG 76540 --- [           main] s.m.mapper.HotelMapper.selectByCityId    : ==> Parameters: 1(Integer)
2019-05-17 01:01:01.724 TRACE 76540 --- [           main] s.m.mapper.HotelMapper.selectByCityId    : <==    Columns: CITY, NAME, ADDRESS, ZIP
2019-05-17 01:01:01.724 TRACE 76540 --- [           main] s.m.mapper.HotelMapper.selectByCityId    : <==        Row: 1, Conrad Treasury Place, William & George Streets, 4001
2019-05-17 01:01:01.725 DEBUG 76540 --- [           main] s.m.mapper.HotelMapper.selectByCityId    : <==      Total: 1

If you don't need query results, change the log level to DEBUG.

It also is possible to set different log level per mapper/statement.
Please see the doc for details.

ave
  • 3,244
  • 2
  • 14
  • 20