代码实现
public PageDataDomain<DataSourceDataFindAllBySourceId> displayDate(Integer pageNumber, Integer pageSize, String keyWord, Long dataSourceId) {
PageDataDomain<DataSourceDataFindAllBySourceId> pageDataDomain = new PageDataDomain<>();
Sort sort = Sort.by(Sort.Direction.DESC,"id");
PageRequest pageRequest = PageRequest.of(pageNumber-1,pageSize,sort);
Specification<DataSourceData> specification = new Specification<DataSourceData>() {
@Override
public Predicate toPredicate(Root<DataSourceData> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate restrictions = criteriaBuilder.conjunction();
if(dataSourceId != null ){
Path path = root.get("dataSourceId");
Predicate pre = criteriaBuilder.equal(path, dataSourceId);
restrictions = criteriaBuilder.and(restrictions,pre);
}
if(keyWord != null){
Path chineseName = root.get("chineseName");
Predicate pd = criteriaBuilder.like(chineseName, keyWord);
restrictions = criteriaBuilder.and(restrictions,pd);
}
Predicate predicate = criteriaBuilder.and(restrictions);
return predicate;
}
};
Page<DataSourceData> dataSourceDataPage = dataSourceDataRepository.findAll(specification, pageRequest);
//总记录数
pageDataDomain.setTotal(dataSourceDataPage.getTotalElements());
//当前页
pageDataDomain.setCurrent(pageNumber);
pageDataDomain.setSize(pageSize);
//总条数
pageDataDomain.setPages(dataSourceDataPage.getTotalPages());
dataSourceDataPage.getContent().forEach(dataSourceData -> {
DataSourceDataFindAllBySourceId dataSourceDataFindAllBySourceId = new DataSourceDataFindAllBySourceId();
BeanUtils.copyProperties(dataSourceData, dataSourceDataFindAllBySourceId);
pageDataDomain.getRecords().add(dataSourceDataFindAllBySourceId);
});
return pageDataDomain;
}