java 报错:传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。
解决方法:
一、代码实现
@Service
public class HumanInfoServiceImpl implements HumanInfoService {
@Autowired
private HumanInfoMapper humanInfoMapper;
/**
* 查询传入的参数超过2000
* @param humanIdList 传入的人员编号集合(人员编号集合超过2000)
* @return
*/
@Override
public List<Object> findObject(List<String> humanIdList){
//临时存放人员编号集合
List<String> tempList = new ArrayList<>();
//得到返回的结果
List<Object> objectList = ParamUtil.batchParams(humanIdList,tempList,humanInfoMapper,
"selectHumanInfo",tempList);
return objectList;
}
/**
* 新增传入的参数超过2000
* @param humanInfoList 传入的新增对象 新增的参数超过2000
*/
@Override
public void insertObject(List<HumanInfo> humanInfoList){
//临时集合
List<HumanInfo> tempList = new ArrayList<>();
//执行新增语句
ParamUtil.batchInsertParams(humanIdList,tempList,humanInfoMapper,
"insertHumanInfo",tempList);
}
}
二、编写工具类
(1)、获取类里面对应的方法名
package com.test.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.Optional;
/**
* 用反射获取类里面的方法名
*/
public class ReflectionUtils {
private static final Logger logger = LoggerFactory.getLogger(ReflectionUtils.class);
/**
* @description 方法调用指定对象方法
* @param object 传入反射对象
* @param method 传入反射对象的方法名
* @param args 反射对象的方法参数
* @return
*/
public static Object invokeMethod(Object object,String method,Object...args) throws Exception {
Object result = null;
//反射方法获取到类
Class<? extends Object> classObj = object.getClass();
//
Method queryMethod = getMethod(classObj,method);
if (Optional.ofNullable(queryMethod).isPresent()){
result = queryMethod.invoke(object,args);
}else {
throw new Exception(classObj.getName() + "类中没有找到"+method+"方法。");
}
return result;
}
/**
* 反射对象中的反射方法集合中是否存在对应的方法
* @param classObj 反射对象
* @param name 反射方法名
* @return
*/
public static Method getMethod( Class<? extends Object> classObj,String name){
Method queryMethod = null;
Method[] methods = classObj.getMethods();
for (Method method : methods) {
if (method.getName().equals(name)) {
queryMethod = method;
break;
}
}
return queryMethod;
}
}
(2)、分2000为一次执行
package com.test.util;
import java.util.ArrayList;
import java.util.List;
public class ParamUtil {
private static final Integer PARAM_NUM = 2000;
/**
* 查询返回的结果,查询的参数超过2000
* @param paramIdList 传入的参数集合
* @param tempList 临时集合
* @param mapper 对象名
* @param queryMethod 对象对应的方法名
* @param args 传入的参数
* @return 查询的结果
*/
public static List<Object> batchParams(List<Object> paramIdList,List<Object> tempList,
Object mapper,String queryMethod,Object...args) throws Exception {
List<Object> resultObject = new ArrayList<>();//存放返回对象集
if (paramIdList == null || paramIdList.isEmpty()){
return resultObject;
}
int paraIdSize = paramIdList.size();
int a = paraIdSize / PARAM_NUM; //得到商
int b = paraIdSize % PARAM_NUM; //得到余数
if (b > 0){
a = a + 1;
}
//循环a的次数
for (int i = 0 ;i < a;i++){
tempList.clear();
for (int j = PARAM_NUM * i ; j < PARAM_NUM * (i+1) ; j++){
if (j >= paraIdSize){
break;
}
tempList.add(paramIdList.get(j));
}
//得到查询集合
Object result = ReflectionUtils.invokeMethod(mapper,queryMethod,args);
List<Object> objectList = (List)result;
//将查询集合加入resultObject集合中
if (!objectList.isEmpty()){
resultObject.addAll(objectList);
}
}
return resultObject;
}
/**
* 新增的参数超过2000
* @param paramIdList 传入的参数集合
* @param tempList 临时集合
* @param mapper 对象名
* @param queryMethod 对象对应的方法名
* @param args 传入的参数
* @return 查询的结果
*/
public static void batchInsertParams(List<Object> paramIdList,List<Object> tempList,
Object mapper,String queryMethod,Object...args) throws Exception {
if (paramIdList == null || paramIdList.isEmpty()){
return ;
}
int paraIdSize = paramIdList.size();
int a = paraIdSize / PARAM_NUM; //得到商
int b = paraIdSize % PARAM_NUM; //得到余数
if (b > 0){
a = a + 1;
}
//循环a的次数
for (int i = 0 ;i < a;i++){
tempList.clear();
for (int j = PARAM_NUM * i ; j < PARAM_NUM * (i+1) ; j++ ){
if (j >= paraIdSize){
break;
}
tempList.add(paramIdList.get(j));
}
ReflectionUtils.invokeMethod(mapper,queryMethod,args);
}
}
}