1.转page功能
/**
* List 转 Page 对象
*
* @param list
* @param pageable 分页参数
* @param <T>
* @return
*/
public <T> Page<T> convertList2PageVO(final List<T> list, final Pageable pageable) {
if (CollectionUtils.isEmpty(list)) {
return new PageImpl(new ArrayList<>(0), pageable, 0);
}
final List<T> ingredientVOS = list;
final List<List<T>> partition = Lists.partition(list, pageable.getPageSize());
List<T> pageContent = partition.get(pageable.getPageNumber());
return new PageImpl<>(pageContent, pageable, ingredientVOS.size());
}
2.用法
@Override
public Pagination<SupposeEntity> findByStatusList(SupposeDto specialistDto) {
ExampleMatcher matcher = ExampleMatcher.matching();
SupposeEntity matchSpecialist = new SupposeEntity();
if (null != specialistDto.getThesisId()) {
matcher = matcher.withMatcher("thesisId", ExampleMatcher.GenericPropertyMatchers.contains());
matchSpecialist.setThesisId(specialistDto.getThesisId());
}
Example<SupposeEntity> example = Example.of(matchSpecialist, matcher);
Sort sort = new Sort(Sort.Direction.DESC, "id");
PageRequest pageable = new PageRequest(specialistDto.getPage() - 1, specialistDto.getSize(), sort);
Page<SupposeEntity> page = supposeRepository.findAll(example, pageable);
//加了个状态条件
Page<SupposeEntity> supposeEntities = convertList2PageVO(page.stream().
filter(supposeEntity -> supposeEntity.getStatus() == 0).collect(Collectors.toList()), pageable);
return Pagination.convert(supposeEntities);
}
主要关注点:
Page<SupposeEntity> supposeEntities = convertList2PageVO(page.stream().
filter(supposeEntity -> supposeEntity.getStatus() == 0).collect(Collectors.toList()), pageable);