zoukankan      html  css  js  c++  java
  • 分页

    一、原理

    1.MySQL和Oracle分页原理:

    使用MySQL进行分页:

    select * from table limit (pageNo - 1)*pageSize, pageSize;

    需要注意的是 limit 子句需要写在查询语句的最后。

    使用Oracle进行分页:

    (1)最简单的使用:

    select t2.*
    from (
        select rownum r, t1.*
        from table t1
        where rownum < ?
    ) t2
    where  t2.r > ?

    说明:查询小于最大的,大于最小的。随着数据的扩张,查询速度会越来越慢。

    (2)无ORDER BY排序的写法。(效率最高) 

    SELECT * 
    FROM (Select ROWNUM AS ROWNO, T.* 
          from k_task T 
          where Flight_date between to_date('20060501', 'yyyymmdd') 
          and to_date('20060731', 'yyyymmdd') 
          AND ROWNUM <= 20) TABLE_ALIAS 
    WHERE TABLE_ALIAS.ROWNO >= 10;

    (3)有ORDER BY排序的写法。(效率最高,但是也会随着数据的扩张而越来越慢)

    SELECT * 
    FROM (SELECT TT.*, ROWNUM AS ROWNO 
          FROM (
              Select t.* 
              from k_task T 
              where flight_date between to_date('20060501', 'yyyymmdd') 
              and to_date('20060531', 'yyyymmdd') 
              ORDER BY FACT_UP_TIME, flight_no) TT 
           WHERE ROWNUM <= 20) TABLE_ALIAS 
    where TABLE_ALIAS.rowno >= 10;

    2.创建Page类(基础)

    封装的数据:要显示的数据的集合/每页显示多少条记录/总记录数/总页数/当前的页码/是否存在上一页——当前页和1进行比较/是否存在下一页——当前页和总页数进行比较/上一页页码/下一页页码/首页:1/末页:总页数

    数据的来源:数据库:数据集合,总记录数;内部指定:每页显示多少条记录,可以指定为常量;内部计算:总页数,页面传入,当前页的页码

    public class Page<T> {
        // 每页显示多少条记录
        public static final int PAGE_SIZE = 3;
        // 要显示的数据
        private List<T> list;
        // 总记录数
        private int totalRecord;
        // 总页数
        private int totalPageNo;
        // 当前页
        private int pageNo;
    
        public Page(String pageNoStr, int totalRcord) {
            // 赋值总记录数
            this.totalRecord = totalRcord;
            
            // 根据总记录数计算总页数
            this.totalPageNo = this.totalPageNo / PAGE_SIZE + ((this.totalPageNo % PAGE_SIZE == 0) ? 0 : 1);
            
            // 指定 pageNo 默认值
            this.pageNo = 1;
            
            // 对pageNo进行赋值
            try {
                this.pageNo  = Integer.parseInt(pageNoStr);
            } catch (NumberFormatException e) {}
            
            // 对pageNo进行校正
            if(this.pageNo > this.totalPageNo) {
                this.pageNo = this.totalPageNo;
            }
            
            if(this.pageNo < 1) {
                this.pageNo = 1;
            }
            
        }
    
        // 是否有上一页
        public boolean isHasPrev() {
            return this.pageNo > 1;
        }
    
        // 是否有下一页
        public boolean isHasNext() {
            return this.pageNo < this.totalPageNo;
        }
    
        // 上一页
        public int getPrev() {
            return this.pageNo - 1;
        }
    
        // 下一页
        public int getNext() {
            return this.pageNo + 1;
        }
    
        public List<T> getList() {
            return list;
        }
    
        public void setList(List<T> list) {
            this.list = list;
        }
    
        public int getTotalRecord() {
            return totalRecord;
        }
    
        public int getTotalPageNo() {
            return totalPageNo;
        }
    
        public int getPageNo() {
            return pageNo;
        }
    
    }
    Page

    在使用该Page类进行查询的时候,需要查询两次数据库。一次查询总记录数,另一次查询分页列表内容记录。

    具体使用:

    (1)只根据PageNo进行查询

    查询总记录数:

    public int getTotalRecord() {
        String sql = "select count(*) from book";
        long record = this.getSingleValue(sql);
        return (int) record;
    }
    getTotalRecord

    查询List:

    public List<Book> getPageList(int pageNo, int pageSize) {
        String sql = "select book_id bookId, book_name bookName, author, price, store_num storeNum, "
                + "salse_amount salseAmount, img_path imgPath, category_id categoryId from book limit ?, ?";
        return this.getBeanList(sql, (pageNo - 1)*pageSize , pageSize);
    }
    getPageList

    使用:

    int totalRcord = bookDao.getTotalRecord();
    Page<Book> page = new Page<Book>(pageNoStr, totalRcord);
    List<Book> list = bookDao.getPageList(page.getPageNo(), Page.PAGE_SIZE);
    page.setList(list);
    use

    (2)带查询条件的分页

    将查询条件封装为一个对象。

    查询总记录数:

    public int getTotalRecord(PageCondition pageCondition) {
        String sql = "select count(*) from book where price <= ? and price >= ? ";
        if(pageCondition.getCategoryId() != null) {
            sql += "and category_id =" + pageCondition.getCategoryId();
        }
        long record = this.getSingleValue(sql, pageCondition.getMaxPrice(), pageCondition.getMinPrice());
        return (int) record;
    }
    getTotalRecord

    查询List:

    public List<Book> getPageList(PageCondition pageCondition, int pageNo, int pageSize) {
        String sql = "select book_id bookId, book_name bookName, author, price, store_num storeNum, "
                + "salse_amount salseAmount, img_path imgPath, category_id categoryId from book "
                + "where price <= ? and price >= ? ";
        if(pageCondition.getCategoryId() != null) {
            sql += "and category_id = " + pageCondition.getCategoryId();
        }
        sql += " limit ?, ?";
        return this.getBeanList(sql, pageCondition.getMaxPrice(), pageCondition.getMinPrice(),(pageNo - 1)*pageSize , pageSize);
    }
    getPageList

    使用:

    @Override
    public Page<Book> getPage(PageCondition pageCondition) {
        int totalRcord = bookDao.getTotalRecord(pageCondition);
        Page<Book> page = new Page<Book>(pageCondition.getPageNoStr(), totalRcord);
        List<Book> list = bookDao.getPageList(pageCondition, page.getPageNo(), Page.PAGE_SIZE);
        page.setList(list);
        return page;
    }
    getPage

    未完,待续。

  • 相关阅读:
    Linux mail命令详解
    Linux 硬件RAID详解系统功能图
    Linux 下Discuz论坛的搭建
    Linux 下Wordpress博客搭建
    运维监控---企业级Zabbix详解_【all】
    Linux下的Mysql的双向同步
    Linux下的Mysql的主从备份
    实参时丢弃了类型 discards qualifiers discards qualifiers问题
    Qt::ConnectionType(信号与槽的传递方式)
    Qt多线程编程总结(一)
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5633427.html
Copyright © 2011-2022 走看看