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

相关推荐
前行的小黑炭10 分钟前
Android LayoutInflater 是什么?XML到View的过程
android·java·kotlin
尚久龙19 分钟前
安卓学习 之 SeekBar(音视频播放进度条)
android·java·学习·手机·android studio
要一起看日出20 分钟前
Shiro概述
java·spring boot·java-ee
不秃的开发媛23 分钟前
Java开发入门指南:IDE选择与数据库连接详解
java·数据库·ide
没有bug.的程序员30 分钟前
Redis Sentinel:高可用架构的守护者
java·redis·架构·sentinel
Zhen (Evan) Wang31 分钟前
.NET 6 文件下载
java·前端·.net
王嘉俊92544 分钟前
SpringBoot应用开发指南:从入门到高级配置与自动装配原理
java·spring boot·后端·spring·ssm
孤廖1 小时前
【算法磨剑:用 C++ 思考的艺术・Dijkstra 实战】弱化版 vs 标准版模板,洛谷 P3371/P4779 双题精讲
java·开发语言·c++·程序人生·算法·贪心算法·启发式算法
赵谨言1 小时前
基于数据挖掘技术构建电信5G客户预测模型的研究与应用
经验分享·5g·数据挖掘·毕业设计