From 449d2c6da795bbec60b2367e54db56702992e279 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Sun, 6 Nov 2022 19:36:59 +0800 Subject: [PATCH] fix Parameter 'paramType_Gr8re1Ee' not found --- .../yulichang/config/InterceptorConfig.java | 95 ++++++++++--------- .../yulichang/interceptor/MPJInterceptor.java | 6 +- .../yulichang/toolkit/InterceptorList.java | 49 ++++++++++ 3 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/github/yulichang/toolkit/InterceptorList.java diff --git a/src/main/java/com/github/yulichang/config/InterceptorConfig.java b/src/main/java/com/github/yulichang/config/InterceptorConfig.java index 882fb70..5263176 100644 --- a/src/main/java/com/github/yulichang/config/InterceptorConfig.java +++ b/src/main/java/com/github/yulichang/config/InterceptorConfig.java @@ -1,69 +1,72 @@ package com.github.yulichang.config; -import com.baomidou.mybatisplus.core.injector.ISqlInjector; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.github.yulichang.exception.MPJException; -import com.github.yulichang.injector.MPJSqlInjector; -import com.github.yulichang.interceptor.MPJInterceptor; +import com.github.yulichang.toolkit.InterceptorList; import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.InterceptorChain; -import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.ApplicationListener; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.annotation.Configuration; import java.lang.reflect.Field; import java.util.List; -import java.util.Objects; /** - * 拦截器配置类 如果配置了分页插件,可能会使拦截器失效 - * 此类的作用就是校验拦截器顺序,保证连表插件在其他拦截器之前执行 + * 兼容 page helper 插件类 + *

+ * 可以自定义Conditional注解简化代码 todo * * @author yulichang */ -@SuppressWarnings("NullableProblems") -public class InterceptorConfig implements ApplicationListener { +@Configuration +@SuppressWarnings("unused") +public class InterceptorConfig { private static final Log logger = LogFactory.getLog(InterceptorConfig.class); - @Autowired(required = false) - private List sqlSessionFactoryList; - @Autowired(required = false) - private MPJInterceptor mpjInterceptor; - @Autowired(required = false) - private ISqlInjector iSqlInjector; - - @Override - @SuppressWarnings("unchecked") - public void onApplicationEvent(ApplicationReadyEvent event) { - if (CollectionUtils.isNotEmpty(sqlSessionFactoryList) && Objects.nonNull(mpjInterceptor)) { - try { - for (SqlSessionFactory factory : sqlSessionFactoryList) { - Field interceptorChain = Configuration.class.getDeclaredField("interceptorChain"); - interceptorChain.setAccessible(true); - InterceptorChain chain = (InterceptorChain) interceptorChain.get(factory.getConfiguration()); - Field interceptors = InterceptorChain.class.getDeclaredField("interceptors"); - interceptors.setAccessible(true); - List list = (List) interceptors.get(chain); - if (CollectionUtils.isNotEmpty(list)) { - if (list.get(list.size() - 1) != mpjInterceptor) { - list.removeIf(i -> i == mpjInterceptor); - list.add(mpjInterceptor); - } - } else { - list.add(mpjInterceptor); - } - } - } catch (Exception ignored) { - throw new MPJException("mpjInterceptor exception"); - } + @Configuration + @ConditionalOnBean(type = "com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration") + @AutoConfigureBefore(name = {"com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration"}) + public static class PhSpringBoot { + public PhSpringBoot(List sqlSessionFactoryList) { + replaceInterceptorChain(sqlSessionFactoryList); } - if (iSqlInjector != null && !(iSqlInjector instanceof MPJSqlInjector)) { - logger.error("sql注入器未继承 MPJSqlInjector -> " + iSqlInjector.getClass()); + } + + @Configuration + @ConditionalOnBean(type = "com.github.pagehelper.PageInterceptor") + @AutoConfigureBefore(name = {"com.github.pagehelper.PageInterceptor"}) + public static class PhSpring { + public PhSpring(List sqlSessionFactoryList) { + replaceInterceptorChain(sqlSessionFactoryList); + } + } + + @SuppressWarnings("unchecked") + public static void replaceInterceptorChain(List sqlSessionFactoryList) { + if (CollectionUtils.isEmpty(sqlSessionFactoryList)) { + return; + } + for (SqlSessionFactory factory : sqlSessionFactoryList) { + try { + Field interceptorChain = org.apache.ibatis.session.Configuration.class.getDeclaredField("interceptorChain"); + interceptorChain.setAccessible(true); + InterceptorChain chain = (InterceptorChain) interceptorChain.get(factory.getConfiguration()); + Field interceptors = InterceptorChain.class.getDeclaredField("interceptors"); + interceptors.setAccessible(true); + List list = (List) interceptors.get(chain); + if (CollectionUtils.isEmpty(list)) { + interceptors.set(chain, new InterceptorList<>()); + } else { + interceptors.set(chain, new InterceptorList<>(list)); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + logger.error("初始化 MPJ 拦截器失败"); + e.printStackTrace(); + } } } } diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 5e77b52..cd1fa0b 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -8,8 +8,8 @@ import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.ReflectionKit; -import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.toolkit.support.SelectColumn; +import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; @@ -39,8 +39,11 @@ import java.util.concurrent.ConcurrentHashMap; * * @author yulichang */ +@SuppressWarnings("unchecked") @Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})) public class MPJInterceptor implements Interceptor { + + private static final Log logger = LogFactory.getLog(MPJInterceptor.class); private static final List EMPTY_RESULT_MAPPING = new ArrayList<>(0); @@ -55,6 +58,7 @@ public class MPJInterceptor implements Interceptor { */ private static final boolean printResultMap = false; + @Override @SuppressWarnings({"Java8MapApi", "unchecked"}) public Object intercept(Invocation invocation) throws Throwable { diff --git a/src/main/java/com/github/yulichang/toolkit/InterceptorList.java b/src/main/java/com/github/yulichang/toolkit/InterceptorList.java new file mode 100644 index 0000000..be052d0 --- /dev/null +++ b/src/main/java/com/github/yulichang/toolkit/InterceptorList.java @@ -0,0 +1,49 @@ +package com.github.yulichang.toolkit; + +import com.github.yulichang.interceptor.MPJInterceptor; +import org.apache.ibatis.plugin.Interceptor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.function.Predicate; + +/** + * mybatis 拦截器列表 + * 用于替换 interceptorChain 中的拦截器列表 + * 保证 MPJInterceptor 再最后一个(第一个执行) + * + * @author yulichang + * @since 1.2.5 + */ +public class InterceptorList extends ArrayList { + + public InterceptorList() { + super(); + } + + public InterceptorList(Collection c) { + super(c); + Predicate predicate = i -> i instanceof MPJInterceptor; + if (this.stream().anyMatch(predicate)) { + E mpjInterceptor = super.stream().filter(predicate).findFirst().orElse(null); + super.removeIf(predicate); + super.add(mpjInterceptor); + } + } + + @Override + public boolean add(E e) { + if (this.isEmpty()) { + return super.add(e); + } + Predicate predicate = i -> i instanceof MPJInterceptor; + if (this.stream().anyMatch(predicate)) { + E mpjInterceptor = super.stream().filter(predicate).findFirst().orElse(null); + super.removeIf(predicate); + boolean a = super.add(e); + boolean b = super.add(mpjInterceptor); + return a && b; + } + return super.add(e); + } +}