1,service層代碼:ide
@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,外人不要看了,沒意義spa
2,轉換封裝成須要的modelPagingcode
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,返回的modelblog
public class QrCodeRecordPaging extends PageModel<QrCodeRecordModel> { }