mybatis-plus项目中使用mybatis插件

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();
    }
}

4.之前在使用tk-mybatis的老项目中,mybatis插件的配置类中需要向SqlSessionFactory中注入mybatis插件。而新的mybatis-plus项目中按照mybatis-plus设置SqlSessionFactory的形式接入mybatis插件,反而会导致使用mybatis-plus的地方报Invalid bound statement (not found)。

因此在mybatis-plus项目中使用和只使用mybatis的项目中,mybatis插件的配置形式可能有所不同。这个需要根据是否对一些组件进行自定义封装。

相关推荐
CV工程师丁Sir2 小时前
Rokid设备连接全解析:蓝牙与Wi-Fi通信源码深度剖析
java
zoyation2 小时前
多线程简介和在JAVA中应用
java·开发语言
rechol2 小时前
类与对象(中)笔记整理
java·javascript·笔记
周杰伦_Jay2 小时前
【Spring Boot从入门到精通】原理、实战与最佳实践
java·spring boot·后端
呼哧呼哧.2 小时前
SpringBoot 的入门开发
java·spring boot·后端
观望过往2 小时前
【Java数据结构】队列详解与经典 OJ 题目实战
java·数据结构
天地人-神君3 小时前
将.idea取消git托管
java·git·intellij-idea
譕痕3 小时前
Idea 启动报 未找到有效的 Maven 安装问题
java·maven·intellij-idea
Mr YiRan3 小时前
多线程性能优化基础
android·java·开发语言·性能优化
CHEN5_023 小时前
【leetcode100】和为k的子数组(两种解法)
java·数据结构·算法