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 + "]";
        }
        
    }
  • 相关阅读:
    未在本地计算机上注册“Microsoft.Ace.OleDb.12.0”提供程序解决办法
    禁止复制 + 锁右键 + 禁止全选(兼容IE Chrome等)
    Oracle面试题
    SQL面试题
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 16: invalid start byte
    Python3 迭代器与生成器
    Python3 循环_break和continue语句及循环中的else子句
    Python3 编程第一步_关键字end
    Python3 编程第一步_斐波纳契数列_连续赋值
    Linux系统管理_主题02 :管好文件(1)_2.4 链接文件_ln
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10516170.html
Copyright © 2011-2022 走看看