分页在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";
}

上一篇:2021-2022-1 20212804《Linux内核原理与分析》第八周作业


下一篇:LG V60解锁Bootloader教程