java代码所写的分页demo示例不用连接mysql


/**
 * @author gzj
 */
public class PageDemo {
    public static void main(String[] args) {
        // 从前端获取页码和页大小和查询条件
        Page page = frontData(4, 10, "a");
        System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
        page = frontData(4, 10, "b");
        System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
        page = frontData(4, 10, "c");
        System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
        page = frontData(4, 10, "d");
        System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
        page = frontData(4, 10, "e");
        System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
    }

    /**
     * 根据页码和页大小和查询条件查询数据
     *
     * @param frontCondition 查询条件
     * @param pageNow        页码
     * @param pageSize       页大小
     * @return 后端返回给前端的数据
     */
    private static BackPage handleQuery(String frontCondition, int pageNow, int pageSize) {
        // 条件查询 共多少数据
        int total = conditionSelectCount(frontCondition);
        // 余数
        int remainder = total % pageSize;
        // 总共多少页
        int pageCount = remainder == 0 ? total / pageSize : (total / pageSize) + 1;
        // 页码
        pageNow = (pageNow * pageSize) > total ? pageCount : pageNow;
        // 计算limit起始位置
        int currentPage = pageNow > 0 ? (pageNow - 1) * pageSize : pageNow * pageSize;
        // 怕页码超出界限了 超出界限就通过currenPage (3 * 10) > total (25) ? currentPage (20) - (pageSize (10) - remainder (3)) : currentPage
        int startPage = currentPage > total ? currentPage - (pageSize - remainder) : currentPage;
        // 查询数据
        String data = conditionSelect(frontCondition, startPage, pageSize, total);
        // 返回前端
        BackPage backPage = new BackPage(pageNow, pageSize, total, data, pageCount);
        return backPage;
    }


    /**
     * 模拟查询
     *
     * @param condition 查询条件
     * @param total
     * @return 查询后得出来的具体数据
     */
    public static String conditionSelect(String condition, int start, int pageSize, int total) {
        if ("a".equals(condition)) {
            return "按条件a 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
        }
        if ("b".equals(condition)) {
            return "按条件b 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
        }
        if ("c".equals(condition)) {
            return "按条件c 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
        }
        if ("d".equals(condition)) {
            return "按条件d 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
        }
        return "其他条件 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
    }

    /**
     *
     * @param start
     * @param pageSize
     * @param total
     * @return
     */
    private static String createData(int start, int pageSize, int total) {
        StringBuilder sb = new StringBuilder(" ,数据: ");
        if (total > 0) {
            // 找到比较小的充当数据最大值   因为start 是limit的起始位置 而pageSize是前端指定的页大小 total表示就这些数据
            int maxData = (start + pageSize) > total ? total : (start + pageSize);
            for (int i = start; i < maxData; i++) {
                sb.append(i + ",");
            }
            return sb.deleteCharAt(sb.length() - 1).toString();
        }
        return sb.toString();
    }

    /**
     * 模拟条件查询后的数据共多少条
     *
     * @param condition 条件
     * @return total 共多少条
     */
    public static int conditionSelectCount(String condition) {
        // 先查询这个条件能查出来多少条数据 select count(*) from table_name where condition
        if ("a".equals(condition)) {
            return 54;
        }
        if ("b".equals(condition)) {
            return 33;
        }
        if ("c".equals(condition)) {
            return 11;
        }
        if ("d".equals(condition)) {
            return 0;
        }
        return 5;
    }


    /**
     * 模拟前端传参
     *
     * @return
     */
    public static Page frontData() {
        return new Page();
    }

    /**
     * 模拟前端传参
     *
     * @param pageNow   第几页
     * @param pageSize  一页多大
     * @param condition 查询条件
     * @return 封装前端传参
     */
    public static Page frontData(int pageNow, int pageSize, String condition) {
        return new Page(pageNow, pageSize, condition);
    }

    public static class Page {
        int pageSize = 10;
        int pageNow = 1;

        String condition = "a";

        public Page() {

        }

        /**
         * @param pageNow   第几页
         * @param pageSize  页大小
         * @param condition 查询条件
         */
        public Page(int pageNow, int pageSize, String condition) {
            this.pageSize = pageSize;
            this.pageNow = pageNow;
            this.condition = condition;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getPageNow() {
            return pageNow;
        }

        public void setPageNow(int pageNow) {
            this.pageNow = pageNow;
        }

        public String getCondition() {
            return condition;
        }

        public void setCondition(String condition) {
            this.condition = condition;
        }
    }


    public static class BackPage {
        private int pageNow;
        private int pageSize;
        private int pageCount;
        private String data;

        private int total;

        /**
         * @param pageNow   当前页
         * @param pageSize  页大小啊
         * @param total     共多少条
         * @param data      查询的数据
         * @param pageCount 共多少页
         */
        public BackPage(int pageNow, int pageSize, int total, String data, int pageCount) {
            this.pageNow = pageNow == 0 ? 1 : pageNow;
            this.pageSize = pageSize;
            this.pageCount = pageCount;
            this.total = total;
            this.data = data;
        }

        @Override
        public String toString() {
            return "BackPage{" +
                    "pageNow=" + pageNow +
                    ", pageSize=" + pageSize +
                    ", pageCount=" + pageCount +
                    ", data='" + data + '\'' +
                    ", total=" + total +
                    '}';
        }
    }
}

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值