• JavaWeb学习笔记(十九)—— 分页


    一、MySQL中的分页

    格式:select * from 表 limit ?,?;
        参数1:开始索引start,默认值:0。必须是正数
        参数2:每页显示个数 pageSize
        
    例如:
     select * from products limit 0,5; #第一页,每页显示5条
     select * from products limit 5,5; #第二页,每页显示5条
     select * from products limet 10,5; #第三页.每页显示5条
     select * from products limit ?,5; #第currentPage页,每页显示5条
     
        start = (currentPage-1)*pageSize;

    二、PageBean的设计

    public class PageBean {
        // 当前页数(浏览器传递)
        private Integer currentPage;
        // 每页显示条数(固定值,也可以是浏览器传递)
        private Integer pageSize;
        // 总记录数(数据库查询)
        private Integer totalCount;
        // 总页数
        private Integer totalPage;
        // 分页列表数据(数据库查询)
        private List list;
        
        public PageBean(Integer currentPage,Integer totalCount,Integer pageSize){
            this.totalCount=totalCount;
            this.pageSize=pageSize;
            this.currentPage=currentPage;
            if(this.currentPage==null){
                // 如果页面没有指定显示哪一页,显示第一页
                this.currentPage=1;
            }
            if(this.pageSize==null){
                // 如果页面没有指定显示条数,显示3条
                this.pageSize=3;
            }
            
            // 计算总页数
            this.totalPage=(this.totalCount+this.pageSize-1)/this.pageSize;
            
            // 判断当前页数是否超出范围
            // 不能小于1
            if(this.currentPage<1){
                this.currentPage=1;
            }
            
            // 不能大于总页数
            if(this.currentPage>this.totalPage){
                this.currentPage=this.totalPage;
            }
        }
        
        // 计算起始索引
        public int getStart(){
            return (this.currentPage-1)*this.pageSize;
        }
        
        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 getTotalCount() {
            return totalCount;
        }
        public void setTotalCount(Integer totalCount) {
            this.totalCount = totalCount;
        }
        public Integer getTotalPage() {
            return totalPage;
        }
        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }
        public List getList() {
            return list;
        }
        public void setList(List list) {
            this.list = list;
        }
        @Override
        public String toString() {
            return "PageBean [currentPage=" + currentPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount
                    + ", totalPage=" + totalPage + ", list=" + list + "]";
        }
        
    }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Java实现 蓝桥杯VIP 算法提高 师座操作系统
    Qt_5_3_MSVC2012-编译QFtp-qt5编译QFtp
    存储入门 – RAID技术(大图解释)
    为什么要关闭360云盘:新来的美工嫌我们logo太丑,所以就决定关闭了。这个理由怎么样
    360云盘服务端前来回答
    SMB2 Protocol – 简介(应用层协议主要用于在计算机间共享文件、打印机、串口等)
    MVC 5
    集合
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10516170.html
Copyright © 2020-2023  润新知