SpringBoot中Mybatis记录执行sql日志
MyBatis默认使用SLF4J作为日志门面,所以需要在项目中选择一个SLF4J兼容的日志实现,比如Logback, Log4j2等。以Logback为例,也是SpringBoot默认日志框架。
- 添加日志依赖:
确保pom.xml中包含Logback依赖(SpringBoot项目不用添加logback):
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
- 配置Logback:
在src/main/resources目录下,创建logback-spring.xml或则logback.xml配置文件(如果不存在):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="com.example.yourproject.mapper" level="DEBUG" />
<logger name="org.mybatis.spring.SqlSessionTemplate" level="DEBUG" />
<logger name="org.springframework.jdbc.core.JdbcTemplate" level="DEBUG" />
</configuration>
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 重新启动Spring Boot应用
愿你我都能在各自的领域里不断成长,勇敢追求梦想,同时也保持对世界的好奇与善意!