Show SQL trace (Springboot+Mybatis)

不打扰是莪最后的温柔 提交于 2021-01-05 06:42:08

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/56167976/show-sql-trace-springbootmybatis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!