成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

通用分頁解決方案:簡化項目開發(fā)中的分頁難題

開發(fā) 項目管理
本文將分享一個通用分頁解決方案,它能夠對任意數(shù)據(jù)表實現(xiàn)分頁,使用方法簡單便捷,有效提升開發(fā)效率。

前言

在實際項目開發(fā)過程中,分頁功能的使用頻率極高,尤其是針對不同數(shù)據(jù)表進行分頁操作,往往是一件頗為繁瑣的事情。不同的數(shù)據(jù)表結構、業(yè)務需求以及數(shù)據(jù)量大小,都可能導致分頁實現(xiàn)方式的差異,這無疑增加了開發(fā)的復雜性和工作量。

本文將分享一個通用分頁解決方案,它能夠對任意數(shù)據(jù)表實現(xiàn)分頁,使用方法簡單便捷,有效提升開發(fā)效率。

實現(xiàn)

/**
 * 通用分頁工具類
 *
 * @param <T>
 */
@Data
public class PageUtils<T> implements Serializable {

    /**
     * 當前頁碼
     */
    private int currentPage;
    /**
     * 每頁大小
     */
    private int pageSize;
    /**
     * 總數(shù)據(jù)條數(shù)
     */
    private int totalNum;

    /**
     * 首頁
     */
    private int first = 1;

    /**
     * 尾頁
     */
    private int last;
    /**
     * 總頁數(shù)
     */
    private int totalPage;

    /**
     * 上一頁
     */
    private int prev;
    /**
     * 下一頁
     */
    private int next;

    /**
     * 頁面序號顯示的起始位置
     */
    private int startNum;

    /**
     * 頁碼顯示控制-開始頁碼
     */
    private int start;
    /**
     * 頁碼顯示控制-結束頁碼
     */
    private int end;
    /**
     * 顯示頁碼控制-總顯示頁碼(防止頁碼過多,頁面顯示擁擠問題)
     */
    private int count = 10;

    /**
     * 數(shù)據(jù)
     */
    private List<T> list = new ArrayList<>();

    /**
     * 在構造器中根據(jù)指定的參數(shù),計算其他所有屬性的屬性值
     *
     * @param currentPage
     * @param pageSize
     * @param totalNum
     */
    public PageUtils(int currentPage, int pageSize, int totalNum) {
        this.currentPage = currentPage;
        //賦值每天顯示的記錄條數(shù)
        this.pageSize = pageSize;
        //賦值總記錄數(shù)(總數(shù)據(jù)條數(shù))
        this.totalNum = totalNum;

        //計算獲得總頁數(shù)以及尾頁
        this.totalPage = this.last = (int) Math.ceil((double) totalNum / pageSize);
        //防止當前頁小于1
        this.currentPage = Math.max(this.currentPage, 1);
        //防止當前頁超過總頁數(shù)
        this.currentPage = Math.min(this.totalPage, this.currentPage);

        //設置上一頁:上一頁不能小于1
        this.prev = Math.max(this.currentPage - 1, 1);
        //設置下一頁:下一頁不能超過總頁數(shù)
        this.next = Math.min(this.currentPage + 1, this.totalPage);

        //計算獲取數(shù)據(jù)顯示的序號位置
        this.startNum = (this.currentPage - 1) * pageSize;
        //計算顯示頁碼的起始位置:起始位置不能小于1
        this.start = Math.max(this.currentPage - this.count / 2, 1);
        //計算顯示頁碼的結束位置:結束位置不能超過總頁數(shù)
        this.end = Math.min(this.start + this.count, this.totalPage);
    }
}

具體使用:

假設這是數(shù)據(jù)訪問對象類,負責與數(shù)據(jù)庫交互

public class DemoDAO {
    // 模擬獲取總數(shù)據(jù)條數(shù)的方法,實際中會執(zhí)行數(shù)據(jù)庫查詢
    public int totalNum() {
        // 這里簡單返回一個固定值模擬數(shù)據(jù),實際應從數(shù)據(jù)庫查詢
        return 100;
    }

    // 模擬根據(jù)頁碼和每頁大小查詢數(shù)據(jù)的方法,實際中會執(zhí)行數(shù)據(jù)庫查詢
    public List<Demo> findByPage(int currentPage, int pageSize) {
        List<Demo> resultList = new ArrayList<>();
        // 計算數(shù)據(jù)起始位置
        int startIndex = (currentPage - 1) * pageSize;
        // 簡單模擬數(shù)據(jù)生成,實際應從數(shù)據(jù)庫查詢
        for (int i = startIndex; i < startIndex + pageSize && i < 100; i++) {
            resultList.add(new Demo(i, "Demo_" + i));
        }
        return resultList;
    }
}

業(yè)務服務類,調用DAO類并使用分頁工具類進行分頁處理

public class DemoService {

    public PageUtils query(int currentPage, int pageSize) {
        DemoDAO dao = new DemoDAO();
        //查總數(shù)據(jù)條數(shù)
        int totalNum = dao.totalNum();
        //根據(jù)提供的參數(shù)構建一個PageUtils對象
        PageUtils<Demo> pu = new PageUtils<>(currentPage, pageSize, totalNum);
        //查當前頁數(shù)據(jù)
        List<Demo> list = dao.findByPage(pu.getCurrentPage(), pu.getPageSize());
        //將查詢到的指定頁碼的數(shù)據(jù)存儲到分頁工具對象中
        pu.setList(list);
        //將分頁工具對象返回
        return pu;
    }
}

測試: 1-10

圖片圖片

3-10

圖片圖片

責任編輯:武曉燕 來源: 一安未來
相關推薦

2021-10-04 09:14:18

ElasticSear深度分頁

2016-10-25 14:18:59

分頁javascriptminiui

2017-02-28 14:28:37

數(shù)據(jù)跨庫分頁架構

2009-07-15 17:00:49

JDBC查詢

2025-03-07 09:01:14

商品模塊接口項目

2019-05-14 14:27:21

跨庫分頁分庫數(shù)據(jù)

2011-03-21 10:35:10

2024-09-26 14:27:14

2011-08-11 18:54:01

數(shù)據(jù)庫分頁查詢

2010-06-10 12:37:27

MySQL分頁查詢

2009-12-23 09:04:41

LINQ通用分頁

2015-09-07 09:52:08

云部署云產(chǎn)品云解決方案

2014-12-25 13:18:39

2013-11-05 10:22:20

瀏覽器加密

2013-03-31 14:10:55

敏捷開發(fā)

2010-06-11 14:41:20

MySQL分頁查詢

2011-03-24 13:31:35

2分法存儲過程分頁

2009-09-15 09:50:47

虛擬化解決方案

2009-09-03 21:36:36

CDN解決方案網(wǎng)絡管理
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产日韩欧美一区二区 | 少妇无套高潮一二三区 | 国产精品一区二区在线 | 好姑娘高清在线观看电影 | 四虎永久免费在线 | 99久久精品国产一区二区三区 | 国产免费xxx | 国产亚洲精品久久久优势 | 在线看亚洲 | 国产精品久久久久久久久久久久 | 欧美久久视频 | 日本在线看 | 精品久久久久久亚洲精品 | 九九热精品在线 | 国产亚洲精品精品国产亚洲综合 | 亚洲国产成人精品女人久久久 | 成人亚洲| 日本一区二区高清视频 | 国产高清一区二区 | 亚洲综合精品 | 欧美日韩在线一区二区 | 亚洲三级免费看 | 亚洲美女在线一区 | 日韩在线一区二区 | 久久久久国产精品午夜一区 | 欧美日韩国产在线 | 亚洲精品区| 成人教育av | 日韩国产在线观看 | 国产91久久久久 | 成人不卡在线 | 日韩视频一区二区 | 久久成人av电影 | 成人av一区二区三区 | 超碰在线播 | 久久精品网| 精产国产伦理一二三区 | 久久精品欧美一区二区三区麻豆 | 亚洲精品乱码久久久久v最新版 | 男女爱爱网站 | 在线成人精品视频 |