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插件的配置形式可能有所不同。这个需要根据是否对一些组件进行自定义封装。

相关推荐
powerfulhell2 分钟前
寒假python作业5
java·前端·python
1尢晞13 分钟前
Java学习
java·开发语言
阿杰真不会敲代码5 分钟前
Mybatis-plus入门到精通
java·tomcat·mybatis
木井巳8 分钟前
【递归算法】二叉搜索树中第K小的元素
java·算法·leetcode·深度优先·剪枝
qq_2975746714 分钟前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
AIGC小火龙果15 分钟前
【出海心路】Claude Code实战心法
经验分享
星辰_mya17 分钟前
Elasticsearch线上问题之慢查询
java·开发语言·jvm
南极星100519 分钟前
我的创作纪念日--128天
java·python·opencv·职场和发展
郝学胜-神的一滴25 分钟前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
摇滚侠26 分钟前
Java,举例说明,函数式接口,函数式接口实现类,通过匿名内部类实现函数式接口,通过 Lambda 表达式实现函数式接口,演变的过程
java·开发语言·python