in关键字中参数超过最大值的解决方法

前台页面查询时报错
在这里插入图片描述
定位到具体sql,放到PL SQL中查询
在这里插入图片描述
也就是in关键字默认参数数量大于1000时,就会报错

解决方法:

/**
*@Description: sql查询中使用in关键字时 参数数量大于900则分段查询
 *@param1: in中参数列表LIST 
 *@param2: 需要查询的字段
 *@author: V_US9Y3S
 *@date: 2020-7-6
 */
public static String getInParameter(List<String> list, String parameter) {
    if (!list.isEmpty()) {
        List<String> setList = new ArrayList<String>(0);
        Set set = new HashSet();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= list.size(); i++) {
            set.add("'" + list.get(i - 1) + "'");
            if (i % 900 == 0) {// 900为阈值
                setList.add(org.apache.commons.lang.StringUtils.join(set.iterator(), ","));
                set.clear();
            }
        }
        if (!set.isEmpty()) {
            setList.add(org.apache.commons.lang.StringUtils.join(set.iterator(), ","));
        }
        stringBuffer.append(setList.get(0));
        for (int j = 1; j < setList.size(); j++) {
            stringBuffer.append(") or " + parameter + " in (");
            stringBuffer.append(setList.get(j));
        }
        return stringBuffer.toString();
    } else {
        return "''";
    }
}
// idList: [308989171, 309365293, 309259039]
String sql1TaskId = methodUtil.getInParameter(idList, "B.TASK_ID"); 
// sql1TaskId: '309365293','308989171','309259039' 
sql1.append(" AND (B.TASK_ID IN ("+sql1TaskId+") )");
// 上面表达式中,如果idList的大小大于900,则超过900的部分会用'or'的方式与900前相连

这里要注意,使用了 “or” 关键字时,最好用括号括起两边sql(另一篇文章里有)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值