sql 查询 in里的个数超过1000 实用方法

引用apache lang的jar包,用到它的StringUtils.


import org.apache.commons.lang.StringUtils;

 /**
     * 
     * @param ids in 语句集合对象
     * @param count in 语句中出现的条件个数
     * @param field in 语句对应的数据库查询字段
     * @return
     */
    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);
            
            String yjdNbr = StringUtils.defaultIfEmpty(StringUtils.join(ids.subList(fromIndex, toIndex), "','"), "");
            if (i != 0) {
                builder.append(" or ");
            }
            builder.append(field).append(" in ('").append(yjdNbr).append("')");
        }
        
        return StringUtils.defaultIfEmpty(builder.toString(), field + " in ('')");
    }

你可能感兴趣的:(JAVA,sql,数据库)