1,service层代码:
@Override public QrCodeRecordPaging getPage(String projectId, Integer pageNumber, Integer pageSize) { //创建时间排序 Sort sort = new Sort(Sort.Direction.DESC, "createAt"); //不合法给予默认值 if (pageNumber == null || pageSize == null || pageNumber <= 0 || pageSize <= 0) { pageNumber = 1; pageSize = Integer.MAX_VALUE; } PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort); //返回分页信息 Page<QrCodeRecord> all = qrCodeRecordRepository.findAll((root, query, cb) -> { //断言,添加断言 Predicate p = cb.equal(root.get("projectId").as(String.class), projectId); return cb.and(p); }, pageRequest); return recordAssembler.toPage(all); }
看一下业务层就好,下面是我们自己公司封装的model,外人不要看了,没意义
2,转换封装成需要的modelPaging
public QrCodeRecordPaging toPage(Page<QrCodeRecord> all) { QrCodeRecordPaging paging = new QrCodeRecordPaging(); List<QrCodeRecordModel> models = new ArrayList<>(); all.forEach(qrCodeRecord -> models.add(toQrCodeRecordModel(qrCodeRecord))); paging.setData(new Items<QrCodeRecordModel>().init(models)); paging.setFirst(all.isFirst()); paging.setHasNext(all.hasNext()); paging.setHasPrevious(all.hasPrevious()); paging.setLast(all.isLast()); paging.setPageNumber(all.getNumber() + 1); paging.setPageSize(all.getSize()); paging.setTotalPage(all.getTotalPages()); paging.setTotalElements(all.getTotalElements()); return paging; }
3,返回的model
public class QrCodeRecordPaging extends PageModel<QrCodeRecordModel> { }