分頁案例詳解

  • 物理分頁

只從數據庫中查詢出要顯示的數據html

優勢:不佔用不少內存java

缺點:速度比較低,每一次都要從數據庫中獲取mysql

  • 邏輯分頁

從數據庫中將全部記錄查找到,存儲到內存中,須要什麼數據sql

直接從內存中獲取.數據庫

優勢:速度比較快瀏覽器

缺點:佔用比較多的內存,若是數據比較多,能夠出現內在溢出。jsp

     數據實時更新須要單獨處理.this

mysqllimit介紹

利用mysqllimit,進行物理分頁。spa

select * from 表名  limit m,n;code

m是從0開始,表明是第幾條記錄   

n表明顯示多少條記錄

例如

select * from person limit 4,10;

從第五條記錄開始,顯示10.

分頁實現原理分析

1.知道一共有多少條記錄

select count(*) from ;

2.知道每一頁顯示多少條記錄

人爲定義的.

3.一共有多少頁

1.總頁數=總條數%每頁條數==0?總條數/每頁條數:總條數/每頁條數+1

2.總頁數=Math.ceil(總條數*1.0/每頁條數);

4.當前頁碼

默認值爲1,表明第一頁.

當點擊上一頁,下一頁,就是對頁碼進行+1 -1操做.

5.須要當前頁的數據

例如:每頁顯示五條,要查詢第三頁數據

select * from limit(3-1)*5,5;

(當前頁碼-1)*每頁條數,就求出了開始的記錄位置,在向下查找每頁數個記錄。就獲得了這頁的數據

 

 

注意:limitmysql數據庫特有的,它能夠至關因而mysql的方言。

 

Sqlserver   top

 

Oracle   rownum

 

------------------------------

 

擴展:咱們能夠經過jdbc寫出脫離數據庫的分頁操做。

 

  1. 將結果集設置成滾動結果集。

 

可使用它的absolute方法將遊標定位到指定位置。

 

.

分頁原理:

  1. 數據一共有幾條

SELECT COUNT(*) FROM products;  一共11條  totalCount

  1. 每頁顯示幾條數據

人爲定義的,假設咱們規定一頁顯示4條數據。 currentCount

  1. 求出一共有多少頁  totalpage

totalpage=Math.ceil(1.0*totalCount/currentCount);   //java中兩個整數int運算結果仍是個整數,因此乘以1.0,就不是整數運算啦,向上取整之後是3

totalpage=totalCount%currentCount==0?: totalCount/currentCount; totalCount/currentCount+1  //除盡等於零

  1. 求出當前頁 currentPage

  默認是第一頁   上一頁就是在當前頁基礎上-1   下一頁就是在當前頁基礎上+1

若是是第一頁,必定沒有上一頁,若是是最後一頁,必定沒有下一頁。

  1. 求出當前頁的數據

Select * from product limit (當前頁-1)*每頁條數,每頁條數;

 

 

首先咱們須要作個分頁javabean

 

import java.util.List;

public class PageBean {

    private int totalCount; // 總條數
    private int totalPage; // 總頁數
    private int currentPage; // 當前頁
    private int currentCount; // 每頁條數
    private List<Product> ps; // 當前頁數據.

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getCurrentCount() {
        return currentCount;
    }

    public void setCurrentCount(int currentCount) {
        this.currentCount = currentCount;
    }

    public List<Product> getPs() {
        return ps;
    }

    public void setPs(List<Product> ps) {
        this.ps = ps;
    }

}

 

使用這個PageBean的目的是爲了將分頁的相關數據攜帶到頁面上。

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body> <div id="divpagecontent"> <table width="100%" border="0" cellspacing="0"> <tr> <td> <div style="text-align:right; margin:5px 10px 5px 0px"> <a href="index.jsp">首頁</a>&nbsp;&nbsp;&nbsp;計算機&nbsp;&nbsp;&nbsp;圖書列表 </div> <table cellspacing="0" class="listcontent"> <tr> <td> <h1>商品目錄</h1> <hr /> <h1>計算機</h1>&nbsp;&nbsp;&nbsp;&nbsp;共100種商品 <hr /> <div style="margin-top:20px; margin-bottom:5px"> <img src="images/productlist.gif" width="100%" height="38" /> </div> <table cellspacing="0" class="booklist"> <tr> <c:forEach items="${bean.ps}" var="p"> <td> <div class="divbookpic"> <p> <a href="#"><img src="" width="115" height="129" border="0" /></a> </p> </div> <div class="divlisttitle"> <a href="#">書名:${p.name}<br />售價:${p.price} </a> </div></td> </c:forEach> </tr> </table> <div class="pagination"> <ul> <c:if test="${bean.currentPage==1}"> <li class="disablepage">&lt;&lt;上一頁</li> </c:if> <c:if test="${bean.currentPage!=1}"> <li><a href="${pageContext.request.contextPath}/listProductByPage?currentPage=${bean.currentPage-1}"> &lt;&lt;上一頁</a> </li> </c:if> <li class="currentpage">1</li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <c:if test="${bean.currentPage==bean.totalPage}"> <li class="disablepage">下一頁&gt;&gt;</li> </c:if> <c:if test="${bean.currentPage!=bean.totalPage}"> <li class="nextPage"> <a href="${pageContext.request.contextPath}/listProductByPage?currentPage=${bean.currentPage+1}"> 下一頁&gt;&gt;</a> </li> </c:if> </ul> </div></td> </tr> </table> </td> </tr> </table> </div> </body> </html>

 

 

public class ListProductByPageServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 1.定義出每頁條數
        int currentCount = 4;// 默認每頁顯示4條件

        // 2.定義出當前頁碼

        int currentPage = 1;// 默認是第一頁

        String _currentPage = request.getParameter("currentPage");
        if (_currentPage != null) {
            currentPage = Integer.parseInt(_currentPage);
            // 若是當前_currentPage不爲null說明瀏覽器端傳遞過來了頁碼,咱們就讓當前的頁碼不在爲1,而是傳遞過來的值
        }

        // 3.調用service中分頁查詢的方法。
        ProductService service = new ProductService();

        try {
            PageBean bean = service.findByPage(currentCount, currentPage);

            request.setAttribute("bean", bean);
            request.getRequestDispatcher("/product_list.jsp").forward(request,
                    response);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
public class ProductService {
    ProductDao dao = new ProductDao();

    // 分頁顯示數據
    // currentCount 每頁條數
    // currentPage 當前頁
    public PageBean findByPage(int currentCount, int currentPage)
            throws SQLException {
        // 1.求出總條數
        int totalCount = dao.findAllCount();
        // 2.求出總頁數
        int totalPage = (int) Math.ceil(1.0 * totalCount / currentCount);
        // 3.求出當前頁的數據
        List<Product> ps = dao.findByPage(currentCount, currentPage);

        PageBean bean = new PageBean();
        bean.setTotalCount(totalCount);
        bean.setTotalPage(totalPage);
        bean.setCurrentCount(currentCount);
        bean.setCurrentPage(currentPage);
        bean.setPs(ps);
        
        return bean;
    }
}
public class ProductDao {
    
    // 求出數據總條數
    public int findAllCount() throws SQLException {
        // 1.建立一個QueryRunner
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        // 2.執行sql
        Long count = (Long) runner.query("select count(*) from products",
                new ScalarHandler());

        return count.intValue();
    }

    // 查詢出當前頁的數據
    // currentCount 每頁條數
    // currentPage 當前頁
    public List<Product> findByPage(int currentCount, int currentPage) throws SQLException {
        // 1.建立一個QueryRunner
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());

        // 2.執行sql
        return runner.query("select * from products limit ?,?",
                new BeanListHandler<Product>(Product.class), (currentPage - 1)
                        * currentCount, currentCount);
    }
}
相關文章
相關標籤/搜索