Mybatis查询使用In突破1000条限制

创建一个数据库工具类

public class OracleSqlUtil {
    public static String forStringIn(String fieldName, List valueList) {
        StringBuilder result = new StringBuilder();
        if(valueList == null || valueList.isEmpty()){
            result.append(fieldName).append(" in ('')");
            return result.toString();
        }
        List group = Lists.partition(valueList, 999);
        if (group.size() > 1) {
            result.append("(");
        }

        Integer initSize = result.length();
        Iterator var5 = group.iterator();

        while (var5.hasNext()) {
            List part = (List) var5.next();
            if (result.length() > initSize) {
                result.append(" or ");
            }

            StringBuilder partSql = new StringBuilder();
            partSql.append(fieldName).append(" in (").append(part.stream().map((value) -> {
                return parseString(value);
            }).collect(Collectors.joining(","))).append(")");
            result.append(partSql);
        }

        if (group.size() > 1) {
            result.append(")");
        }
        return result.toString();
    }


    public static String forStringNotIn(String fieldName, List valueList) {
        StringBuilder result = new StringBuilder();
        if(valueList == null || valueList.isEmpty()){
            result.append(fieldName).append(" not in ('')");
            return result.toString();
        }
        List group = Lists.partition(valueList, 999);
        if (group.size() > 1) {
            result.append("(");
        }

        Integer initSize = result.length();
        Iterator var5 = group.iterator();

        while (var5.hasNext()) {
            List part = (List) var5.next();
            if (result.length() > initSize) {
                result.append(" or ");
            }

            StringBuilder partSql = new StringBuilder();
            partSql.append(fieldName).append(" not in (").append(part.stream().map((value) -> {
                return parseString(value);
            }).collect(Collectors.joining(","))).append(")");
            result.append(partSql);
        }

        if (group.size() > 1) {
            result.append(")");
        }
        return result.toString();
    }

}

在查询的时候,条件这样写

相当于in

${@com.erichfund.settle.base.util.OracleSqlUtil@forStringIn('T.APP_NO', appNoList)}

相当于 not in

${@com.erichfund.xint.base.util.OracleSqlUtil@forStringNotIn('T.APP_NO', appNoList)})

你可能感兴趣的:(Java,mybatis,java,jvm)