fix Parameter 'paramType_Gr8re1Ee' not found

This commit is contained in:
yulichang 2022-11-06 19:36:59 +08:00
parent ece59bb9e5
commit 449d2c6da7
3 changed files with 103 additions and 47 deletions

View File

@ -1,69 +1,72 @@
package com.github.yulichang.config; package com.github.yulichang.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.yulichang.exception.MPJException; import com.github.yulichang.toolkit.InterceptorList;
import com.github.yulichang.injector.MPJSqlInjector;
import com.github.yulichang.interceptor.MPJInterceptor;
import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.InterceptorChain; import org.apache.ibatis.plugin.InterceptorChain;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* 拦截器配置类 如果配置了分页插件,可能会使拦截器失效 * 兼容 page helper 插件类
* 此类的作用就是校验拦截器顺序,保证连表插件在其他拦截器之前执行 * <p>
* 可以自定义Conditional注解简化代码 todo
* *
* @author yulichang * @author yulichang
*/ */
@SuppressWarnings("NullableProblems") @Configuration
public class InterceptorConfig implements ApplicationListener<ApplicationReadyEvent> { @SuppressWarnings("unused")
public class InterceptorConfig {
private static final Log logger = LogFactory.getLog(InterceptorConfig.class); private static final Log logger = LogFactory.getLog(InterceptorConfig.class);
@Autowired(required = false) @Configuration
private List<SqlSessionFactory> sqlSessionFactoryList; @ConditionalOnBean(type = "com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration")
@Autowired(required = false) @AutoConfigureBefore(name = {"com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration"})
private MPJInterceptor mpjInterceptor; public static class PhSpringBoot {
@Autowired(required = false) public PhSpringBoot(List<SqlSessionFactory> sqlSessionFactoryList) {
private ISqlInjector iSqlInjector; replaceInterceptorChain(sqlSessionFactoryList);
@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<Interceptor> list = (List<Interceptor>) 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");
}
} }
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<SqlSessionFactory> sqlSessionFactoryList) {
replaceInterceptorChain(sqlSessionFactoryList);
}
}
@SuppressWarnings("unchecked")
public static void replaceInterceptorChain(List<SqlSessionFactory> 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<Interceptor> list = (List<Interceptor>) 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();
}
} }
} }
} }

View File

@ -8,8 +8,8 @@ import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.method.MPJResultType; import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.ReflectionKit; import com.github.yulichang.toolkit.ReflectionKit;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.toolkit.support.SelectColumn;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.logging.LogFactory;
@ -39,8 +39,11 @@ import java.util.concurrent.ConcurrentHashMap;
* *
* @author yulichang * @author yulichang
*/ */
@SuppressWarnings("unchecked")
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})) @Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class MPJInterceptor implements Interceptor { public class MPJInterceptor implements Interceptor {
private static final Log logger = LogFactory.getLog(MPJInterceptor.class); private static final Log logger = LogFactory.getLog(MPJInterceptor.class);
private static final List<ResultMapping> EMPTY_RESULT_MAPPING = new ArrayList<>(0); private static final List<ResultMapping> EMPTY_RESULT_MAPPING = new ArrayList<>(0);
@ -55,6 +58,7 @@ public class MPJInterceptor implements Interceptor {
*/ */
private static final boolean printResultMap = false; private static final boolean printResultMap = false;
@Override @Override
@SuppressWarnings({"Java8MapApi", "unchecked"}) @SuppressWarnings({"Java8MapApi", "unchecked"})
public Object intercept(Invocation invocation) throws Throwable { public Object intercept(Invocation invocation) throws Throwable {

View File

@ -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<E extends Interceptor> extends ArrayList<E> {
public InterceptorList() {
super();
}
public InterceptorList(Collection<? extends E> c) {
super(c);
Predicate<E> 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<E> 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);
}
}