springboot通过mybatis-plus中的Interceptor⾃定义⼀个拦截
器。。。
1、实现功能结果如下图所⽰,⽤红框框住的就是实现的打印功能,⽽下边带?号的是原始的:
3、要实现此功能需要加⼊如下依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3、将spring-boot-starter-logging从spring-boot-starter-web中排除,如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
4、将原来的mybatis-spring-boot-starter去掉,最后l⽂件为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.best</groupId>
<artifactId>spring-boot-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<fastjson.version>1.2.74</fastjson.version>
<spring.version>5.3.5.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding> </properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<!-- 排除 tomcat-jdbc 以使⽤ HikariCP -->
<exclusion>
<groupId>at</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/dao/*l</include>
</includes>
</resource>
</resources>
</build>
</project>
5、既然是打印⽇志,那么肯定需要⽇志框架,这⾥使⽤的是log4j2,那么就需要再resources中新建⼀个l⽂件,⽂件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--⽇志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后⾯的status,这个⽤于设置log4j2⾃⾝内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够⾃动检测修改配置⽂件和重新配置本⾝,设置间隔秒数-->
<configuration status="WARN" monitorInterval="30">
<properties>
<property name="LOG_HOME">../../../../build/logs</property>
</properties>
<!-- 先定义所有的appender -->
<appenders>
<!-- 这个输出控制台的配置 -->
<Console name="Console" target="SYSTEM_OUT">
<!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 这个都知道是输出⽇志的格式 -->
<PatternLayout charset="UTF-8" pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
</Console>
<!-- ⽂件会打印出所有信息,这个log每次运⾏程序会⾃动清空,由append属性决定,这个也挺有⽤的,适合临时测试⽤ -->
<!-- append为TRUE表⽰消息增加到指定⽂件中,false表⽰消息覆盖指定的⽂件内容,默认值是tr
ue -->
<RollingFile name="DaoFileTrace" fileName="${LOG_HOME}/test.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM}/DaoFileTrace-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<MarkerPatternSelector defaultPattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n">
<PatternMatch key="spark" pattern="%m%n"/>
</MarkerPatternSelector>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<!-- 这个会打印出所有的info及以下级别的信息,每次⼤⼩超过size,则这size⼤⼩的⽇志会⾃动存⼊按年份-⽉份建⽴的⽂件夹下⾯并进⾏压缩,作为存档--> <RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/info.log"
springboot架构图filePattern="${LOG_HOME}/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileWarn" fileName="${LOG_HOME}/warn.log"
filePattern="${LOG_HOME}/${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同⼀⽂件夹下7个⽂件,这⾥设置了20 -->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${LOG_HOME}/error.log"
filePattern="${LOG_HOME}/${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
</appenders>
<!--然后定义logger,只有定义了logger并引⼊的appender,appender才会⽣效-->
<loggers>
<!--过滤掉spring和mybatis的⼀些⽆⽤的DEBUG信息-->
<logger name="org.springframework" level="INFO"></logger>
<logger name="com.alibaba.druid.*" level="INFO" additivity="false">
<AppenderRef ref="Console"/>
</logger>
<logger name="batis" level="INFO" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="DaoFileTrace" />
</logger>
</logger>
<logger name="com.bestmon.sqllog" level="DEBUG" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="DaoFileTrace" />
</logger>
<logger name="org.apache.ibatis.logging" level="INFO" additivity="false">
<AppenderRef ref="Console" />
<AppenderRef ref="DaoFileTrace" />
</logger>
<root level="all">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
6、在添加完l⽂件后,就需要让springboot程序可以加载该配置⽂件,那么只需要再l中加⼊:
logging:
config: l
如下图所⽰:
7、经过以上配置,在springboot中就可以使⽤log4j2框架来打印⽇志了。
8、开始创建,创建⾃定义必须实现Interceptor接⼝,那么我们新建⼀个SqlLoggerInterceptor类并实现Interceptor接⼝,并且通过@Intercepts和@Signature注解来配置拦截的⽅法和对象,这⾥我们拦截的是Executor,也就是mybatis具体执⾏sql语句的对象,⽽Executor对象中有如下⽅法:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论