zoukankan      html  css  js  c++  java
  • 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 + "]";
        }
        
    }
  • 相关阅读:
    iframe页面向上获取父级元素
    解决flex布局 做后一行 靠左的问题
    JavaScript Base64 作为文件上传的实例代码解析
    Python中Flask框架SQLALCHEMY_ECHO设置
    #跟着教程学# 5、python的异常
    #跟着教程学# 4、Python流程控制
    #跟着教程学# 3、Python基础 //Maya select和ls命令返回值问题
    #跟着教程学# 2、Maya Developer Kit下载,及 PyCharm关联Maya
    #跟着教程学# 1、Python_文件批量改名
    (转)maya螺旋线脚本(mel)
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10516170.html
Copyright © 2011-2022 走看看