• 分页在controller/service/dao上各层的写法。


    view:

        @Authorize
        @ApiOperation(value = "学期列表")
        @GetMapping(value = "/list/page")
        public Result<PageBean<BaseSemesterVo>> list(@ApiParam("入口验证") SignModel signModel,
                                                     @ApiParam(value = "第几页", required = true) @RequestParam(defaultValue = AppConstant.CURRPAGE) Integer currPage,
                                                     @ApiParam(value = "每页大小", required = true) @RequestParam(defaultValue = AppConstant.PAGESIZE) Integer pageSize,
                                                     @ApiParam("查询条件") BaseSemesterCondition semesterCondition) throws Exception {
            return ResultGenerator.genSuccessResult(baseSemesterService.findAllByCondition(semesterCondition,currPage, pageSize));
        }
    

    service:

        PageBean<BaseSemesterVo> findAllByCondition(BaseSemesterCondition semesterCondition, Integer currPage, Integer pageSize);
    
    impl#:
        @Override
        public PageBean<BaseSemesterVo> findAllByCondition(BaseSemesterCondition semesterCondition, Integer currPage, Integer pageSize) {
            PageHelper.startPage(currPage, pageSize);
            List<BaseSemesterVo> semesterVoList = baseSemesterMapper.selectAllByCondition(semesterCondition);
            PageBean pageBean = new PageBean(semesterVoList);
            pageBean.setList(semesterVoList);
    
            return pageBean;
        }
    

    dao:

        List<BaseSemesterVo> selectAllByCondition(BaseSemesterCondition semesterCondition);
    

    comm:

    @Data
    public class PageBean<T> implements Serializable {
        private int pageNum;                // 第几页
        private int pageSize;               // 每页记录数
        private int size;                   // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性
        private long total;                 // 总记录数
        private int pages;                  // 总页数
        private List<T> list;               // 结果集
    //    private boolean isFirstPage;        // 是否第一页
    //    private boolean isLastPage;         // 是否最后一页
    //    private boolean hasPreviousPage;    // 是否有上一页
    //    private boolean hasNextPage;        // 是否有下一页
    
        public PageBean() {
    //        this.isFirstPage = false;
    //        this.isLastPage = false;
    //        this.hasPreviousPage = false;
    //        this.hasNextPage = false;
        }
    
        /**
         * 包装Page对象,因为直接返回Page对象,在JSON处理以及其他情况下会被当成List来处理,而出现一些问题。
         *
         * @param list page结果
         */
        public PageBean(List<T> list) {
    //        this.isFirstPage = false;
    //        this.isLastPage = false;
    //        this.hasPreviousPage = false;
    //        this.hasNextPage = false;
            if (list instanceof Page) {
                Page page = (Page) list;
                this.pageNum = page.getPageNum();
                this.pageSize = page.getPageSize();
                this.pages = page.getPages();
                this.list = page;
                this.size = page.size();
                this.total = page.getTotal();
            } else if (list instanceof Collection) {
                this.pageNum = 1;
                this.pageSize = list.size();
                this.pages = 1;
                this.list = list;
                this.size = list.size();
                this.total = (long) list.size();
            }
    
    //        if (list instanceof Collection) {
    //            this.judgePageBoudary();
    //        }
        }
    
    //    private void judgePageBoudary() {
    //        this.isFirstPage = this.pageNum == 1;
    //        this.isLastPage = this.pageNum == this.pages;
    //        this.hasPreviousPage = this.pageNum > 1;
    //        this.hasNextPage = this.pageNum < this.pages;
    //    }
    }
    
    
    
    public interface AppConstant {
        // 第一页
        String CURRPAGE = "1";
    
        // 每页记录数
        String PAGESIZE = "20";
    }
    
    
  • 相关阅读:
    渣渣菜鸡的蚂蚁金服面试经历(一)
    20 个案例教你在 Java 8 中如何处理日期和时间?
    Spring Boot 2.0系列文章(七):SpringApplication 深入探索
    分布式锁看这篇就够了
    Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
    20135337朱荟潼——实验三
    20135337朱荟潼Java实验报告二
    5337朱荟潼Java实验报告一
    Linux内核设计笔记12——内存管理
    Linux内核设计笔记11——定时器
  • 原文地址:https://www.cnblogs.com/kltsee/p/14842118.html
Copyright © 2020-2023  润新知