• JAVA如何实现分页功能
  • 发布于 2个月前
  • 538 热度
    0 评论

前言

最近用JAVA做一个CMS系统,在显示论坛帖子的页面因为帖子太多需要用到分页功能,JAVA菜鸟第一次接触这种分页的需求,捣鼓了半天终于把这个分页功能做好了,这边记录一下分页设计实现过程,加深一下对分页功能的理解。

代码如下:

package com.soyea.util;
import java.io.Serializable;
import java.util.List;
 
/**
 * 堆代码 duidaima.com
 * 分页工具类
 */
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = -8741766802354222579L;
    private int pageSize=5; // 每页显示多少条记录
    private int currentPage=1; //当前第几页数据
    private int totalRecord; // 一共多少条记录
    private int totalPage; // 一共多少页记录
    private List<T> dataList; //要显示的数据
 
    public PageBean(int pageNum, int pageSize, List<T> sourceList) {
        if (sourceList == null || sourceList.isEmpty()) {
            return;
        }
 
        // 总记录条数
        this.totalRecord = sourceList.size();
        // 每页显示多少条记录
        this.pageSize = pageSize;
        //获取总页数
        this.totalPage = this.totalRecord / this.pageSize;
        if (this.totalRecord % this.pageSize != 0) {
            this.totalPage = this.totalPage + 1;
        }
 
        // 当前第几页数据
        this.currentPage = this.totalPage < pageNum ? this.totalPage : pageNum;
        // 起始索引
        int fromIndex = this.pageSize * (this.currentPage - 1);
        // 结束索引
        int toIndex = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord : this.pageSize * this.currentPage;
        this.dataList = sourceList.subList(fromIndex, toIndex);
    }
    public PageBean() {
 
    }
 
    public PageBean(int pageSize, int currentPage, int totalRecord, int totalPage, List<T> dataList) {
        super();
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalRecord = totalRecord;
        this.totalPage = totalPage;
        this.dataList = dataList;
    }
 
    public int getPageSize() {
        return pageSize;
    }
 
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
 
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
 
    public int getTotalRecord() {
        return totalRecord;
    }
 
    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public List<T> getDataList() {
        return dataList;
    }
    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }
 
    @Override
    public String toString() {
        return "PageBean{" +
                "pageSize=" + pageSize +
                ", currentPage=" + currentPage +
                ", totalRecord=" + totalRecord +
                ", totalPage=" + totalPage +
                ", dataList=" + dataList +
                '}';
    }
}
总结:

以上就是关于JAVA如何实现分页功能的逻辑代码实现,整个代码很简单,没有什么太复杂的逻辑,主要是要先去数据库查询获取总记录数,然后根据定义的每页显示条数就可以计算出总的分页数了。

用户评论