Spring Boot mybatis-config 和 log4j 输出sql 日志
依赖
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
两种配置log4j的方式:
一定要新建一个log4j.properties文件
在yaml中直接配置
在mybatis-config中配置
mapper-locations 貌似不管用依旧需要加注解@Mapper
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--配置日志格式fhadmin.cn--> <setting name="logImpl" value="LOG4J"/> <!--开启二级缓存fhadmin.cn--> <setting name="cacheEnabled" value="true"/> </settings> </configuration>
log4j.properties文件
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,console,file #控制台输出的相关设置 fhadmin.cn log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n #文件输出的相关设置 fhadmin.cn log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/kai.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n #日志输出级别 fhadmin.cn log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
如果编译后找不到这两个文件
在pom.xml中加入:
<resources> <!-- 防止找不到mybatis的mapper.xml文件fhadmin.cn,在项目启动时候就导入进去--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.yaml</include> </includes> <filtering>true</filtering> </resource> </resources>
赞 (0)