MyBatis 插件失效

public class BaseMybatisPlusConfig {
    /**
     * Mybatis 实现分页
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        DbType dbType = DbType.MYSQL;
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType));
        return interceptor;
    }
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 注册 Bean
 */
@Component
public class MyBeanFactoryInvoke implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(MyInterceptor.class);
        beanDefinitionRegistry.registerBeanDefinition("MyInterceptor", beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;

import java.util.concurrent.Executor;

@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class})
})
public class MyInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("----------MyInterceptor----------");
        return invocation.proceed();
    }
}
/**
 * MybatisPlus 源码
 */
public MybatisPlusAutoConfiguration(){
	...
	if (!ObjectUtils.isEmpty(this.interceptors)) {
    	factory.setPlugins(this.interceptors);
    }
	...
}

image

执行顺序:先执行 MyInterceptor ,再 通过 invocation.proceed(); 执行 MybatisPlusInterceptor

posted @ 2023-12-14 09:23  笔兴洽谈室  阅读(30)  评论(0编辑  收藏  举报