JPA中动态SQL的查询条件为in中的长度大于1000时 报错处理逻辑:
测试方法:
@Test
public void contextLoads2() {
Integer pageNum = 1; // 当前页
Integer pageSize = 100; // 页大小
// 模拟构造参数
List<String> paramList = Lists.newArrayList();
for (int i = 0; i <= 1999; i++) {
paramList.add(116 + i + "");
}
// 分页处理(从0开始)
Pageable pageable = new PageRequest(pageNum - 1, pageSize);
Page<Emp> page = empDao.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
List<List<String>> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(paramList)) {
int count = 999;
String paraStr = "";
Integer dealCount = paramList.size() % count == 0 ? paramList.size() / count : paramList.size() / count + 1;
for (int i = 0; i < dealCount; i++) {
Integer start = i * count;
Integer end = (i + 1) * count;
if (end > paramList.size()) {
end = paramList.size();
}
list.add(paramList.subList(start, end));
}
}
Predicate[] paramPredicateArr = new Predicate[list.size()];
for (int i = 0; i < list.size(); i++) {
List<String> subList = list.get(i);
CriteriaBuilder.In<String> inId = cb.in(root.get("id"));
subList.forEach(id -> inId.value(id));
Predicate paramPredicate = cb.and(inId);
paramPredicateArr[i] = paramPredicate;
}
Predicate idPredicate = cb.or(paramPredicateArr);
predicates.add(idPredicate);
predicates.add(cb.equal(root.get("checked").as(String.class), "1"));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}, pageable);
List<Emp> empList = page.getContent();
System.out.println(">>>>>empList.size()>>>>>:" + empList.size());
}