显示SQL跟踪(Springboot + Mybatis)

时间:2019-05-16 11:54:56

标签: sql spring spring-boot mybatis

我的项目是使用Spring boot和Mybatis构建的,我想查看sql查询语句,该怎么办?

1 个答案:

答案 0 :(得分:0)

假设您的映射器位于软件包your.pkg.mapper中,请在application.properties中添加以下行,以指示MyBatis打印语句,参数和查询结果。

logging.level.your.pkg.mapper=TRACE

以下是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

如果不需要查询结果,请将日志级别更改为DEBUG

还可以为每个映射器/语句设置不同的日志级别。
有关详细信息,请参见doc