2

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

5

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.