针对oracle执行select in语句超过1000条报错的解决办法

/  例:
//  List<YourResult> allList = mapperBatchExecuter.selectInExecute(queryList, k -> {
//      return yourService.listBy***(k);
//  });
//本方法用于执行oracle数据库查询语句中in的数据量超过1000的查询,
//实际为语法糖,将select in的语句分块执行
    public <U, T> List<T> selectInExecute(List<U> objectList, Function<List<U>, List<T>> function) {
        List<T> allResultList = null;
        Stack<U> queryStack = new Stack<U>();
        List<U> tempQueryList = new ArrayList<>();
        int i = 0;
        boolean lastOne = false;
        if (CollectionUtils.isNotEmpty(objectList)) {
            allResultList = new ArrayList<>();
            queryStack.addAll(objectList);
            while (true) {
                if (!queryStack.isEmpty()) {
                    i++;
                    tempQueryList.add(queryStack.pop());
                } else {
                    lastOne = true;
                }
                if (i > 998 || lastOne) {
                    List<T> tempResultList = function.apply(tempQueryList);
                    if (CollectionUtils.isNotEmpty(tempResultList)) {
                        allResultList.addAll(tempResultList);
                    }
                    tempQueryList.clear();
                    i = 0;
                }
                if (lastOne) {
                    lastOne = false;
                    break;
                }
            }
        }
        return allResultList;
    }

//   传递一个带对象,里面带有一个inList参数的批量查询
    public <U, T> List<T> selectInWithParamExecute(U paramObject, String inListParamName, Function<U, List<T>> function)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
        List<T> allResultList = null;
        Stack<String> queryStack = new Stack<String>();
        List<String> tempQueryList = new ArrayList<>();
        Class<? extends Object> objClazz = paramObject.getClass();
        String upperName = CommonUtils.firstCharToUpperCase(inListParamName);
        String setterName = "set" + upperName;
        String getterName = CommonUtils.firstCharSToG(setterName);
        @SuppressWarnings("unchecked")
        List<String> objectList = (List<String>)objClazz.getMethod(getterName).invoke(paramObject);
        Method setterMethod = objClazz.getMethod(setterName, List.class);
        int i = 0;
        boolean lastOne = false;
        if (CollectionUtils.isNotEmpty(objectList)) {
            allResultList = new ArrayList<>();
            queryStack.addAll(objectList);
            while (true) {
                if (!queryStack.isEmpty()) {
                    i++;
                    tempQueryList.add(queryStack.pop());
                } else {
                    lastOne = true;
                }
                if (i > 998 || lastOne) {
                    setterMethod.invoke(paramObject, tempQueryList);
                    List<T> tempResultList = function.apply(paramObject);
                    if (CollectionUtils.isNotEmpty(tempResultList)) {
                        allResultList.addAll(tempResultList);
                    }
                    tempQueryList.clear();
                    i = 0;
                }
                if (lastOne) {
                    lastOne = false;
                    break;
                }
            }
        }
        return allResultList;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值