在SpringBoot中开启控制台打印MyBatis的执行SQL

626 阅读1分钟

 在调试问题时经常需要打印具体的执行SQL,需要开启SQL打印,这里做下记录

以application.properties文件为例,添加以下配置即可

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

如果是添加了mybatis-spring-boot-starter整合依赖的,直接在配置文件中使用下面方式

logging.level.com.lucifer.springboot.cache.mapper=debug

如果添加的是mybatis-plus或者mybatis-plus-boot-starter整合依赖的话,在配置文件中使用下面方式

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

打印出来的示例如下

Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@69fe8f8a]
JDBC Connection [io.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection@615a03bc] will be managed by Spring
==>  Preparing: SELECT a.template_site_id,a.lv_site_name,a.lv_site_id FROM r_template_site_relate AS a,r_template_site AS b WHERE b.template_id = a.template_id AND a.template_site_id = b.id AND b.template_id = ? GROUP BY a.template_site_id 
==> Parameters: 454(Long)
<==    Columns: template_site_id, lv_site_name, lv_site_id
<==        Row: 3030, 上海, 9
<==        Row: 3031, 北京, 13
<==        Row: 3032, 天津, 14
<==        Row: 3033, 杭州, 69
<==        Row: 3034, 南京, 56
<==        Row: 3035, 常州, 59
<==        Row: 3036, 苏州, 60
<==        Row: 3037, 无锡, 57
<==        Row: 3038, 合肥, 80
<==        Row: 3039, 南昌, 163
<==        Row: 3040, 福州, 154
<==        Row: 3041, 肇庆, 195
<==        Row: 3042, 深圳, 324
<==        Row: 3043, 南宁, 207
<==        Row: 3044, 海口, 221
<==        Row: 3045, 济南, 174
<==        Row: 3046, 中国香港, 40
<==        Row: 3047, 中国澳门, 41
<==        Row: 3048, 中国台北市, 8000
<==        Row: 3049, 甘孜, 222
<==        Row: 3050, 重庆, 31
<==        Row: 3051, 贵阳, 224
<==        Row: 3052, 昆明, 233
<==        Row: 3054, 银川, 362
<==        Row: 3055, 兰州, 340
<==        Row: 3056, 拉萨, 249
<==        Row: 3057, 太原, 100
<==        Row: 3058, 石家庄, 89
<==        Row: 3059, 呼和浩特, 111
<==        Row: 3060, 沈阳, 123
<==        Row: 3061, 哈尔滨, 43
<==        Row: 3062, 长春, 137
<==        Row: 3063, 武汉, 291
<==        Row: 3064, 长沙, 308
<==        Row: 3065, 西安, 256
<==        Row: 3066, 郑州, 191
<==      Total: 36