本文介绍了显示 SQL 跟踪 (Springboot+Mybatis)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

假设您的映射器位于 your.pkg.mapper 包中,请将以下行添加到 application.propertiescode> 告诉 MyBatis 打印语句、参数和查询结果.

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

下面是示例项目.

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.

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

还可以为每个映射器/语句设置不同的日志级别.
详情请参阅doc.

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

这篇关于显示 SQL 跟踪 (Springboot+Mybatis)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:30