只從數據庫中查詢出要顯示的數據html
優勢:不佔用不少內存java
缺點:速度比較低,每一次都要從數據庫中獲取mysql
從數據庫中將全部記錄查找到,存儲到內存中,須要什麼數據sql
直接從內存中獲取.數據庫
優勢:速度比較快瀏覽器
缺點:佔用比較多的內存,若是數據比較多,能夠出現內在溢出。jsp
數據實時更新須要單獨處理.this
利用mysql的limit,進行物理分頁。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)*每頁條數,就求出了開始的記錄位置,在向下查找每頁數個記錄。就獲得了這頁的數據
注意:limit是mysql數據庫特有的,它能夠至關因而mysql的方言。
Sqlserver top
Oracle rownum
------------------------------
擴展:咱們能夠經過jdbc寫出脫離數據庫的分頁操做。
可使用它的absolute方法將遊標定位到指定位置。
.
分頁原理:
SELECT COUNT(*) FROM products; 一共11條 totalCount
人爲定義的,假設咱們規定一頁顯示4條數據。 currentCount
totalpage=Math.ceil(1.0*totalCount/currentCount); //java中兩個整數int運算結果仍是個整數,因此乘以1.0,就不是整數運算啦,向上取整之後是3
totalpage=totalCount%currentCount==0?: totalCount/currentCount; totalCount/currentCount+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> 計算機 圖書列表 </div> <table cellspacing="0" class="listcontent"> <tr> <td> <h1>商品目錄</h1> <hr /> <h1>計算機</h1> 共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"><<上一頁</li> </c:if> <c:if test="${bean.currentPage!=1}"> <li><a href="${pageContext.request.contextPath}/listProductByPage?currentPage=${bean.currentPage-1}"> <<上一頁</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">下一頁>></li> </c:if> <c:if test="${bean.currentPage!=bean.totalPage}"> <li class="nextPage"> <a href="${pageContext.request.contextPath}/listProductByPage?currentPage=${bean.currentPage+1}"> 下一頁>></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); } }