前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
只是一个简单需求: 查询所有部门id 属于 idList 的数据
Page<WorkWeight> page = workWeightRepository.findAll(new Specification<WorkWeight>() {
@Override
public Predicate toPredicate(Root<WorkWeight> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();
if(null != subDepartmentId && !"".equals("subDepartmentId")){
list.add(cb.equal(root.<String>get("subDepartmentId"), subDepartmentId));
}
if(null != status && !"".equals("status")){
list.add(cb.equal(root.<String>get("status"), status));
}
if (null != startTime && !"".equals("startTime")) {
list.add(cb.greaterThanOrEqualTo(root.<Date>get("startTime"), DateUtil.stringToDate(startTime)));
}
if (null != endTime && !"".equals("endTime")) {
list.add(cb.lessThanOrEqualTo(root.<Date>get("endTime"), DateUtil.stringToDate(endTime)));
}
Expression<String> exp = root.<String>get("departmentId");
list.add(exp.in(idList)); // 往in中添加所有id 实现in 查询
if (list.size() != 0) {
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
} else {
return null;
}
}
}, new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()));
重点代码:
Expression<String> exp = root.<String>get("departmentId");
list.add(exp.in(idList)); // idList是部门id集合, 实现in 查询