1. 确保项目添加MyBatis-Plus依赖以及适合的SpringBoot版本。
XML
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>版本号</version>
</dependency>
2. 创建mybatis自定义拦截器(mybatis插件)
java
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
})
@Component
public class MybatisInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
//拦截逻辑
System.out.println("Before update");
Object result = invocation.proceed();
System.out.println("After update");
return result;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
3. 不使用@Component注解,使用配置类
java
@Configuration
public class MybatisInterceptorConfig {
@Bean
public MybatisLogInterceptor mybatisLogInterceptor() {
return new MybatisLogInterceptor();
}
}