日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

分页案例详解

發布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分页案例详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 物理分頁

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

優點:不占用很多內存

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

  • 邏輯分頁

從數據庫中將所有記錄查找到,存儲到內存中,需要什么數據

直接從內存中獲取.

優點:速度比較快

缺點:占用比較多的內存,如果數據比較多,可以出現內在溢出。

?????數據實時更新需要單獨處理.

mysqllimit介紹

利用mysqllimit,進行物理分頁。

select * from 表名 ?limit m,n;

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寫出脫離數據庫的分頁操作。

?

  • 將結果集設置成滾動結果集。
  • ?

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

    ?

    .

    分頁原理:

  • 數據一共有幾條
  • SELECT COUNT(*) FROM products;??一共11條 ?totalCount

  • 每頁顯示幾條數據
  • 人為定義的,假設我們規定一頁顯示4條數據。 currentCount

  • 求出一共有多少頁 ?totalpage
  • totalpage=Math.ceil(1.0*totalCount/currentCount); ? //java中兩個整數int運算結果還是個整數,所以乘以1.0,就不是整數運算啦,向上取整以后是3

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

  • 求出當前頁 currentPage
  • ??默認是第一頁 ??上一頁就是在當前頁基礎上-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.創建一個QueryRunnerQueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());// 2.執行sqlLong 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.創建一個QueryRunnerQueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());// 2.執行sqlreturn runner.query("select * from products limit ?,?",new BeanListHandler<Product>(Product.class), (currentPage - 1)* currentCount, currentCount);} }

    ?

    轉載于:https://www.cnblogs.com/MessiAndDream/p/6081954.html

    總結

    以上是生活随笔為你收集整理的分页案例详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。