分页案例详解
- 物理分頁
只從數(shù)據(jù)庫中查詢出要顯示的數(shù)據(jù)
優(yōu)點(diǎn):不占用很多內(nèi)存
缺點(diǎn):速度比較低,每一次都要從數(shù)據(jù)庫中獲取
- 邏輯分頁
從數(shù)據(jù)庫中將所有記錄查找到,存儲(chǔ)到內(nèi)存中,需要什么數(shù)據(jù)
直接從內(nèi)存中獲取.
優(yōu)點(diǎn):速度比較快
缺點(diǎn):占用比較多的內(nèi)存,如果數(shù)據(jù)比較多,可以出現(xiàn)內(nèi)在溢出。
?????數(shù)據(jù)實(shí)時(shí)更新需要單獨(dú)處理.
mysql中limit介紹
利用mysql的limit,進(jìn)行物理分頁。
select * from 表名 ?limit m,n;
m是從0開始,代表是第幾條記錄 ??
n代表顯示多少條記錄
例如
select * from person limit 4,10;
從第五條記錄開始,顯示10條.
分頁實(shí)現(xiàn)原理分析
1.知道一共有多少條記錄
select count(*) from 表;
2.知道每一頁顯示多少條記錄
人為定義的.
3.一共有多少頁
1.總頁數(shù)=總條數(shù)%每頁條數(shù)==0?總條數(shù)/每頁條數(shù):總條數(shù)/每頁條數(shù)+1
2.總頁數(shù)=Math.ceil(總條數(shù)*1.0/每頁條數(shù));
4.當(dāng)前頁碼
默認(rèn)值為1,代表第一頁.
當(dāng)點(diǎn)擊上一頁,下一頁,就是對頁碼進(jìn)行+1 -1操作.
5.需要當(dāng)前頁的數(shù)據(jù)
例如:每頁顯示五條,要查詢第三頁數(shù)據(jù)
select * from 表 limit(3-1)*5,5;
用(當(dāng)前頁碼-1)*每頁條數(shù),就求出了開始的記錄位置,在向下查找每頁數(shù)個(gè)記錄。就得到了這頁的數(shù)據(jù)
?
?
注意:limit是mysql數(shù)據(jù)庫特有的,它可以相當(dāng)于是mysql的方言。
?
Sqlserver ??top
?
Oracle ??rownum
?
------------------------------
?
擴(kuò)展:我們可以通過jdbc寫出脫離數(shù)據(jù)庫的分頁操作。
?
?
可以使用它的absolute方法將游標(biāo)定位到指定位置。
?
.
分頁原理:
SELECT COUNT(*) FROM products;??一共11條 ?totalCount
人為定義的,假設(shè)我們規(guī)定一頁顯示4條數(shù)據(jù)。 currentCount
totalpage=Math.ceil(1.0*totalCount/currentCount); ? //java中兩個(gè)整數(shù)int運(yùn)算結(jié)果還是個(gè)整數(shù),所以乘以1.0,就不是整數(shù)運(yùn)算啦,向上取整以后是3
totalpage=totalCount%currentCount==0?: totalCount/currentCount; totalCount/currentCount+1 ?//除盡等于零
??默認(rèn)是第一頁 ??上一頁就是在當(dāng)前頁基礎(chǔ)上-1 ??下一頁就是在當(dāng)前頁基礎(chǔ)上+1
如果是第一頁,一定沒有上一頁,如果是最后一頁,一定沒有下一頁。
Select * from product limit (當(dāng)前頁-1)*每頁條數(shù),每頁條數(shù);
?
?
首先我們需要做個(gè)分頁javabean
?
import java.util.List;public class PageBean {private int totalCount; // 總條數(shù)private int totalPage; // 總頁數(shù)private int currentPage; // 當(dāng)前頁private int currentCount; // 每頁條數(shù)private List<Product> ps; // 當(dāng)前頁數(shù)據(jù).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;}}?
使用這個(gè)PageBean的目的是為了將分頁的相關(guān)數(shù)據(jù)攜帶到頁面上。
?
<%@ 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> 計(jì)算機(jī) 圖書列表</div><table cellspacing="0" class="listcontent"><tr><td><h1>商品目錄</h1><hr /><h1>計(jì)算機(jī)</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 />售價(jià):${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.定義出每頁條數(shù)int currentCount = 4;// 默認(rèn)每頁顯示4條件// 2.定義出當(dāng)前頁碼int currentPage = 1;// 默認(rèn)是第一頁 String _currentPage = request.getParameter("currentPage");if (_currentPage != null) {currentPage = Integer.parseInt(_currentPage);// 如果當(dāng)前_currentPage不為null說明瀏覽器端傳遞過來了頁碼,我們就讓當(dāng)前的頁碼不在為1,而是傳遞過來的值 }// 3.調(diào)用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();// 分頁顯示數(shù)據(jù)// currentCount 每頁條數(shù)// currentPage 當(dāng)前頁public PageBean findByPage(int currentCount, int currentPage)throws SQLException {// 1.求出總條數(shù)int totalCount = dao.findAllCount();// 2.求出總頁數(shù)int totalPage = (int) Math.ceil(1.0 * totalCount / currentCount);// 3.求出當(dāng)前頁的數(shù)據(jù)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 {// 求出數(shù)據(jù)總條數(shù)public int findAllCount() throws SQLException {// 1.創(chuàng)建一個(gè)QueryRunnerQueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());// 2.執(zhí)行sqlLong count = (Long) runner.query("select count(*) from products",new ScalarHandler());return count.intValue();}// 查詢出當(dāng)前頁的數(shù)據(jù)// currentCount 每頁條數(shù)// currentPage 當(dāng)前頁public List<Product> findByPage(int currentCount, int currentPage) throws SQLException {// 1.創(chuàng)建一個(gè)QueryRunnerQueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());// 2.執(zhí)行sqlreturn runner.query("select * from products limit ?,?",new BeanListHandler<Product>(Product.class), (currentPage - 1)* currentCount, currentCount);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/MessiAndDream/p/6081954.html
總結(jié)
- 上一篇: C# 操作office知识点汇总
- 下一篇: UML用例图总结来源于网络