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

相关推荐
忙碌的菠萝4 分钟前
使用Apache Lucene构建高效的全文搜索服务
apache·mybatis·lucene
非 白6 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼12 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒15 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
Forget the Dream44 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek1 小时前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
Habibei2 小时前
Mybatis的分页插件
sql·mybatis