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

相关推荐
数智启示录7 分钟前
PostgreSQL 内存调优实战(第 12 篇):work_mem 只调大 64 倍,峰值为什么远不止 64 倍
运维·数据库·经验分享·postgresql·面试
秋名RG19 分钟前
Java 核心特性一览
java·开发语言
霸道流氓气质23 分钟前
Spring AI 技术细节:RAG QuestionAnswerAdvisor 设计与实现
java·人工智能·spring
斑马1391 小时前
Linux软件编程学习笔记(十二)——TCP并发服务器模型
java·服务器·网络
冬栈程序设计1 小时前
Springboot宿舍管理系统【670099】 -附源码(开箱即用)
java·毕业设计·springboot·课程设计·程序设计·大作业
数字生命卡兹克1 小时前
GPT-6 Astra全面解析 - “欢迎来到AGI时代。”
经验分享
2601_962284501 小时前
北京Java全栈开发培训 Web前端开发 软件测试就业培训班
java·软件测试·web前端·全栈开发·就业培训
qiaosaifei1 小时前
JAVA老项目中有大量的类名_编译时报找不到符号错误
java·开发语言
聪明蛋子哟2 小时前
MCP协议深度落地:如何用Python/Java双语言实现统一的工具调用网关?
java·开发语言·python
文子越来越强2 小时前
【双因子认证】拦截注解@MFACheck 接入指南(其他微服务如何用)
java