美文网首页
分页思想以及工具

分页思想以及工具

作者: Apple_Boy | 来源:发表于2019-02-27 15:29 被阅读0次

分页思想:前台分页和后台分页

前台分页

1、提高性能,在某些情况下,我们并不需要查询很大的数据量,在这种情况下,我们在程序中开辟一个内存(其实就是自己定义一个变量~),专门用来存放查询数据,这样每次点击上一页,下一页,就不必每次和数据库进行交互

后台分页

1、提高性能,一次查20个,比一次查20000个性能肯定更好;另外如果数据量很大,一次性将内容都查询出来,查询出来的结果是放在内存里面的,内存没有这么大

2、不需要这么多数据,如新闻,一般人可能只看最近前20条;如果我们将后面的也都查询出来了,就是浪费

3、展现层面的考虑:如果一次展现太多的数据,不管是排版,还是美观上都不好

任何工具都存在优点与缺点,关键在于如何根据实际情况选择恰当的分页方式~

分页信息可以封装在后台,也可以封装在前端

后台:将所有信息直接封装成java代码,显示在前端

前端:从后端获取


日常开发中,web项目基本上都会用到动态分页功能,下面给大家整理写一个完整的PageBean工具类,大家把完整代码拷到自己项目里就可以直接用,废话不多说,直接上代码

import java.util.List;

public class PageBean <T>{

private List<T> pageData;

private Integer currentPage = Integer.valueOf(1);

private Integer pageSize = Integer.valueOf(10);

private Integer totalCount;

    public int getPageCount(){

if (this.totalCount.intValue() % this.pageSize.intValue() == 0) {

    return this.totalCount.intValue() / this.pageSize.intValue();

}

    return this.totalCount.intValue() / this.pageSize.intValue() + 1;

    }


    public PageBean(List<T> pageData, Integer totalCount) {

    this.pageData = pageData;

    this.totalCount = totalCount;

    }


    public PageBean() {}


    public boolean isFirst()

    {

        return (this.currentPage.intValue() == 1) || (this.totalCount.intValue() == 0);

    }


    public boolean isLast() {

    return (this.totalCount.intValue() == 0) || (this.currentPage.intValue() >= getPageCount());

    }

    public boolean isHasNext()

    {

    return this.currentPage.intValue() < getPageCount();

    }


    public boolean isHasPrev() {

    return this.currentPage.intValue() > 1;

    } 

    public Integer getNextPage()

    {

    if (this.currentPage.intValue() >= getPageCount()) {

      return Integer.valueOf(getPageCount());

      }

    return Integer.valueOf(this.currentPage.intValue() + 1);

    }


    public Integer getPrevPage() {

    if (this.currentPage.intValue() <= 1) {

      return Integer.valueOf(1);

      }      

    return Integer.valueOf(this.currentPage.intValue() - 1);

    }


    public List<T> getPageData() {

    return this.pageData;

    }


    public void setPageData(List<T> pageData) {

    this.pageData = pageData;

    }


    public Integer getCurrentPage() {

    return this.currentPage;

    }


    public void setCurrentPage(Integer currentPage) {

    this.currentPage = currentPage;

    }


    public Integer getPageSize() {

    return this.pageSize;

    }


    public void setPageSize(Integer pageSize) {

    this.pageSize = pageSize;

    }


    public Integer getTotalCount() {

    return this.totalCount;

    }


    public void setTotalCount(Integer totalCount) {

    this.totalCount = totalCount;

    }

}

项目service中,只需要根据参数page和pageSize这2个参数,就可以查出符合条件的记录(List<T>),和总数totalCount,把这2个参数传入PageBean这个构造器中即可返回分页的所有数据,代码示例如下:

:最终返回的数据结构如下:

{ "data":{

        "first": true,

        "hasNext": false,

        "hasPrev": false,

        "last": true,

        "nextPage": 1,

        "currentPage": 1,

        "pageCount": 1,

        "pageSize": 20,

        "prevPage": 1,

        "totalCount ": 3,

        "pageData":[{

            "voipId ":"2142343254353456436",

            "voipStateDate ":"2018-04-23 14:02:00",

            "objName":"guoxiansheng",

            "voipStatus":1

        }]

  },

  "errorCode":10000,

  "erorInfo":"创建成功"

}

相关文章

网友评论

      本文标题:分页思想以及工具

      本文链接:https://www.haomeiwen.com/subject/xarfuqtx.html