jpa多条件クエリー,ファジイクエリー,ソート,Javaでlistをページングし,subList()メソッドでページングを実現する
public PageInfo<Pipeline> list2(Integer page, Integer size, Boolean deleted, PipelineSearchInput input) throws Exception {
String name = input.getName();
PipelineState state = input.getState();
String description = input.getDescription();
String start = input.getStart();
String stop = input.getStop();
page = page < 0 ? 0 : page;
size = size < 1 ? 1 : size;
// Pageable pageable = new PageRequest(page, size);
Specification<Pipeline> specification = new Specification<Pipeline>() {
@Override
public Predicate toPredicate(Root<Pipeline> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate deleted = cb.equal(root.get("deleted"), false);
List<Predicate> list = new ArrayList<Predicate>();
list.add(deleted);
if (state != null && !state.equals("") && !StringUtils.isEmpty(state.toString())) {
list.add(cb.equal(root.get("state"), state));
}
if (!StringUtils.isEmpty(name)) {
list.add(cb.like(root.get("name"), "%"+name+"%"));
}
if (!StringUtils.isEmpty(description)) {
list.add(cb.like(root.get("description"), "%"+description+"%"));
}
if (!StringUtils.isEmpty(start)) {
Date startAt = new Date(Long.parseLong(start));
list.add(cb.greaterThanOrEqualTo(root.get("cratedAt"), startAt));
}
if (!StringUtils.isEmpty(stop)) {
Date stopAt = new Date(Long.parseLong(stop));
list.add(cb.lessThanOrEqualTo(root.get("cratedAt"), stopAt));
}
query.orderBy(cb.desc(root.get("cratedAt")));
Predicate[] p = new Predicate[list.size()];
return cb.and(list.toArray(p));
}
};
// Page pipelinepage = repository.findAll(specification, pageable);
List<Pipeline> pipelineList = repository.findAll(specification);
// , PipelineState
List<Pipeline> newPipelineList = pipelineList.stream().sorted(Comparator.comparing(Pipeline::getState).thenComparing(Pipeline::getStartAt).thenComparing(Pipeline::getExecAt)).collect(Collectors.toList());
Collections.reverse(newPipelineList); //
Integer count = newPipelineList.size(); //
Integer pageCount = 0; //
if (count % size == 0) {
pageCount = count / size;
} else {
pageCount = count / size + 1;
}
int fromIndex = 0; //
int toIndex = 0; //
if (page > pageCount - 1) {
page = pageCount - 1;
}
if (!page.equals(pageCount - 1)) {
fromIndex = page * size;
toIndex = fromIndex + size;
} else {
fromIndex = page * size;
toIndex = count;
}
List<Pipeline> subList = newPipelineList.subList(fromIndex, toIndex);
Pageable pageable = new PageRequest(page, size);
Page<Pipeline> pipelinePage = new PageImpl<>(subList, pageable, count);
return new PageInfo<>(pipelinePage);
}
参照先:https://blog.csdn.net/qq_39889838/article/details/82789026 https://blog.csdn.net/qq_39403545/article/details/86319575 https://blog.csdn.net/YoungLee16/article/details/103731251 https://blog.csdn.net/You_are_my_Mr_Right/article/details/103029268 https://www.cnblogs.com/lxxcn/p/12517768.html