• JPA简单的分页条件查询


    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> {
    }
  • 相关阅读:
    python第三十二课——队列
    python第三十二课——栈
    python提示警告InsecureRequestWarning
    关于requests.exceptions.SSLError: HTTPSConnectionPool
    python第三十一课--递归(3.递归的弊端)
    01 redis特点及安装使用
    22 nginx配置与集群
    21-nginx单机1W并发优化
    20-ab压力测试及nginx性能统计模块
    19 大网站的优化思路
  • 原文地址:https://www.cnblogs.com/ljw-bim/p/9208026.html
Copyright © 2020-2023  润新知