zoukankan      html  css  js  c++  java
  • 后端分页操作-springboot (所有类型的数据通用)

    springboot项目少不了对数据做统计报表,这个时候就会涉及到前后端分页操作。一般前端给后端传递当前页数和每页数据条数即可。

    后端来实现分页的所有操作。最近有碰到,所有的操作总结如下:

    一:pom文件中引入依赖。

    <dependency>

           <groupId>com.github.pagehelper</groupId>

           <artifactId>pagehelper-spring-boot-starter</artifactId>

           <version>1.2.7</version>

    </dependency>

     二:实体类文件夹下定义PageBean工具类(我的项目中是写在domains文件夹下)

    package com.fykj.cloud.epaper.entity.bean;
    import java.util.List;
    
    /**
     * 分页bean
     */
    public class PageBean<T> {
        // 当前页
        private Integer currentPage = 1;
        // 每页显示的总条数
        private Integer pageSize = 10;
        // 总条数
        private Integer totalNum;
        // 是否有下一页
        private Integer isMore;
        // 总页数
        private Integer totalPage;
        // 开始索引
        private Integer startIndex;
        // 分页结果
        private List<T> items;
    
        public PageBean() {
            super();
        }
    
        public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
            super();
            this.currentPage = currentPage;
            this.pageSize = pageSize;
            this.totalNum = totalNum;
            this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
            this.startIndex = (this.currentPage-1)*this.pageSize;
            this.isMore = this.currentPage >= this.totalPage?0:1;
        }
        public PageBean(Integer currentPage, Integer pageSize, Integer totalNum,List<T> list){
            this(currentPage,pageSize, totalNum);
            this.items = list;
        }
    
        public Integer getCurrentPage() {
            return currentPage;
        }
    
        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }
    
        public Integer getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    
        public Integer getTotalNum() {
            return totalNum;
        }
    
        public void setTotalNum(Integer totalNum) {
            this.totalNum = totalNum;
            this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
            this.startIndex = (this.currentPage-1)*this.pageSize;
            this.isMore = this.currentPage >= this.totalPage?0:1;
        }
    
        public Integer getIsMore() {
            return isMore;
        }
    
        public void setIsMore(Integer isMore) {
            this.isMore = isMore;
        }
    
        public Integer getTotalPage() {
            return totalPage;
        }
    
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
    
        public Integer getStartIndex() {
            return startIndex;
        }
    
        public void setStartIndex(Integer startIndex) {
            this.startIndex = startIndex;
        }
    
        public List<T> getItems() {
            return items;
        }
    
        public void setItems(List<T> items) {
            this.items = items;
        }
    }
    3.分页功能代码实现(service层及serviceImpl层)
    service层
    public PageBean<UserBlackList> selectUserBlackListList(Integer currentPage, Integer pageSize, UserBlackList userBlackList) 

      service实现层

    @Override
        public PageBean<UserBlackList> selectUserBlackListList(Integer currentPage, Integer pageSize, UserBlackList userBlackList) {
            try {
                PageBean<UserBlackList> entity = new PageBean();
                if (null != pageSize) {
                    entity.setPageSize(pageSize);
                }
                if (null != currentPage) {
                    entity.setCurrentPage(currentPage);
                }
                PageHelper.startPage(entity.getCurrentPage(), entity.getPageSize());
                List<UserBlackList> list = userBlackListMapper.queryModelList(userBlackList);//获取需要分页的所有数据
                Integer count = userBlackListMapper.queryModelListCount(userBlackList);//获取分页的数据总数
                entity.setItems(list);
                entity.setTotalNum(count);
                return entity;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
  • 相关阅读:
    [51nod1685]第k大区间
    [51nod1515]明辨是非
    centos中jdk配置 平凡的人生
    centos中apachetomcat的配置 平凡的人生
    centos6.5中gitlab的搭建 平凡的人生
    centos精简系统 源码安装客户端git 平凡的人生
    Vue学习笔记vue调试工具vuedevtools安装及使用
    Django Admin 实现三级联动的示例代码(省市区)===>小白级
    Vue学习笔记基于CDN引入方式简单前后端分离项目学习(Vue+Element+Axios)
    Django Admin 配置和定制基本功能(基本二次开发配置)
  • 原文地址:https://www.cnblogs.com/maowuyu-xb/p/13476007.html
Copyright © 2011-2022 走看看