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

相关推荐
雷渊28 分钟前
深入分析mybatis中#{}和${}的区别
java·后端·面试
亦是远方34 分钟前
2025华为软件精英挑战赛2600w思路分享
android·java·华为
花月C1 小时前
Spring IOC:容器管理与依赖注入秘籍
java·开发语言·rpc
ylfhpy1 小时前
Java面试黄金宝典22
java·开发语言·算法·面试·职场和发展
风象南1 小时前
Spring Boot 实现文件秒传功能
java·spring boot·后端
橘猫云计算机设计1 小时前
基于django优秀少儿图书推荐网(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·小程序·django·毕业设计
黑猫Teng2 小时前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
星河浪人2 小时前
Spring Boot启动流程及源码实现深度解析
java·spring boot·后端
佩奇的技术笔记2 小时前
中级:Maven面试题精讲
java·面试·maven
Lizhihao_2 小时前
JAVA-堆 和 堆排序
java·开发语言