1. 程式人生 > >oracle中in的個數超過1000的解決辦法

oracle中in的個數超過1000的解決辦法

oracle中in的個數要是超過1000就會報錯,那麼我們可以把它拆成一節一節的:

in(1,2) or in (3,4)

StringUtils.defaultIfEmpty的名稱空間是:

import org.apache.commons.lang.StringUtils;

private String getOracleSQLIn(List<?> ids, int count, String field) {
    count = Math.min(count, 1000);
    int len = ids.size();
    int size = len % count;
    if (size == 0) {
        size = len / count;
    } else {
        size = (len / count) + 1;
    }
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < size; i++) {
        int fromIndex = i * count;
        int toIndex = Math.min(fromIndex + count, len);
        //System.out.println(ids.subList(fromIndex, toIndex));
        String productId = StringUtils.defaultIfEmpty(StringUtils.join(ids.subList(fromIndex, toIndex), "','"), "");
        if (i != 0) {
            builder.append(" or ");
        }
        builder.append(field).append(" in ('").append(productId).append("')");
    }
    
    return StringUtils.defaultIfEmpty(builder.toString(), field + " in ('')");
}
剩下的你懂得!!!!!大笑