常見的分頁方式
1.概念
物理分頁:利用資料庫本身提供的分頁方式,如mysql的limit,oracle的rownum,好處是效率高,不好的地方就是不同的資料庫有不同的搞法。如hibernate採用的是物理分頁。
邏輯分頁:利用遊標分頁,好處是資料庫相容,壞處是效率太低。
真分頁:確定要顯示的頁面內容數量,按需查詢,效率高,但訪問資料庫頻繁,大型網站都用真分頁。
假分頁:一次性從資料庫查出所有的資料並在頁面上顯示。
2.常見的分頁實現方式
sql:
MySQL :SELECT * FROM tablename LIMIT M,N;(M從哪裡開始,N資料的條數)
Oracle: select * from (select s.* ,rownum rn from (select * from tablename) s where rownum<=10) where rn>==1;
儲存過程
create or replace package testpackage as type test_cursor is ref cursor; end testpackage; create or replace procedure fenye (tableName in varchar2,pageSize in number, pageNow in number,myrows out number, myPageCount out number,p_cursor out testpackage.test_cursor) is v_sql varchar2(1000); v_begin number:=(pageNow-1)*pageSize+1; v_end number:=pageNow*pageSize; begin v_sql:='select * from (select t1.*,ROWNUM rn from (select * from'||tableName||') t1 where rownum<='||v_end||') where rn>='||v_begin; open p_cursor for v_sql; v_sql:='select count(*) from '||tableName; execute immediate v_sql into myrows; if mod(myrows,pageSize)=0 then myPageCount:=myrows/pageSize; else myPageCount:=myrows/pageSize+1; end if; close p_cursor; end;
雖然手寫sql可以解決問題,但不利於程式碼的重用。幸好已經有人幫我們封裝了mybatis分頁工具PageHelper,只需做相應配置就可以呼叫分頁。
a.新增maven依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser --> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.5</version> </dependency>
b.在mybatis配置檔案中配置外掛
<plugins>
<!-- mybatis分頁外掛 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="oracle" />
</plugin>
</plugins>
c.service層程式碼
package com.wang.ff.entity.sUser.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wang.ff.entity.sUser.domain.SUser;
import com.wang.ff.entity.sUser.persistence.SUserMapper;
import com.wang.ff.util.BaseService;
@Service
public class UserService extends BaseService<SUserMapper, SUser>{
public SUser getUserByName(String userName){
return this.dao.getUserByName(userName);
}
/**
* 分頁查詢使用者資訊
* @param page
* @return
*/
public PageInfo<SUser> getAllUsers(Page<SUser> page){
if(page.getPageNum()==0){
page.setPageNum(1);
}
String order = page.getOrderBy();
PageHelper.startPage(page.getPageNum(), page.getPageSize(), order);
List<SUser> list = this.dao.selectAll();
PageInfo<SUser> info = new PageInfo<SUser>(list);
return info;
}
}
d.controller程式碼
package com.wang.ff.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.wang.ff.entity.sUser.domain.SUser;
import com.wang.ff.entity.sUser.service.UserService;
@Controller
@RequestMapping(value="/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping(value="/getUser")
public ModelAndView getUser(Page<SUser> page){
page.setPageSize(5);
page.setOrderBy("ID DESC");
PageInfo<SUser> pageInfo = this.userService.getAllUsers(page);
ModelAndView mv = new ModelAndView("user");
mv.addObject("pageInfo",pageInfo);
return mv;
}
}
e.use.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" session="false" pageEncoding="UTF-8"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<table>
<thead>
<th>序號</th>
<th>姓名</th>
<th>密碼</th>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="item">
<tr>
<td>${item.id}</td>
<td>${item.userName}</td>
<td>${item.password}</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td colspan="3" align="center">
<c:if test="${pageInfo.hasPreviousPage}">
<a href="#" onclick="javascript:conditionPageQuery(${pageInfo.prePage});">上一頁</a>
</c:if>
<c:forEach items="${pageInfo.navigatepageNums}" var="curretPageNum" varStatus="status">
<c:choose>
<c:when test="${curretPageNum==pageInfo.pageNum}">
<li class="disabled"><a href="#" >${curretPageNum}</a></li>
</c:when>
<c:otherwise>
<li><a href="#" onclick="javascript:conditionPageQuery(${curretPageNum});">${curretPageNum}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pageInfo.hasNextPage}">
<a href="#" onclick="javascript:conditionPageQuery(${pageInfo.nextPage});">下一頁</a>
</c:if>
</td>
</tr>
</tfoot>
</table>
</body>
</html>
<script type="text/javascript">
function conditionPageQuery(currentPageNum){
window.location.href="${ctx}/user/getUser?pageNum="+currentPageNum;
}
</script>
好吧,你不能指望一個做後臺的能把頁面搞得多麼炫……
相關推薦
幾種常見SQL分頁方式效率比較
har n) over mage 適用於 not blog toolbar 大數 1.創建測試環境,(插入100萬條數據大概耗時5分鐘)。 create database DBTestuse DBTest--創建測試表create table pagetest(id
幾種常見SQL分頁方式
第一種方法:效率最高 [sql] view plain copy SELECT TOP 頁大小 *
常見的分頁方式
1.概念 物理分頁:利用資料庫本身提供的分頁方式,如mysql的limit,oracle的rownum,好處是效率高,不好的地方就是不同的資料庫有不同的搞法。如hibernate採用的是物理分頁。 邏輯分頁:利用遊標分頁,好處是資料庫相容,壞處是效率太低。 真分頁:確定要顯
Asp.Net中的三種分頁方式總結
rom chang clas 綁定 select proc dll xtend tinc 本人ASP.net初學,網上找了一些分頁的資料,看到這篇文章,沒看到作者在名字,我轉了你的文章,只為我可以用的時候方便查看,2010的文章了,不知道這技術是否過期。 以下才是正文
sqlserver的四種分頁方式
pro 支持 分享 class eat cnblogs ext 5 學習 log 第一種:ROW_NUMBER() OVER()方式 select * from ( select *, ROW_NUMBER() OVER(Order by ArtistId )
mysql 、pgsql、oracle 常見分頁
// 總頁數 int pages=0; // 查詢總行數的SQL String rowSql= "select count(*) from "+ table_name; ...... // 判斷頁數,如果是頁大小的整數倍就為rows/pageSize如果不是整數倍就為rows/pageS
兩種分頁方式
分頁查詢兩種方式之 Offset ...Rows Fetch Next ... Rows only Offset ...Rows Fetch Next ... Rows only 方式 在2012後採用這種方式越來越多 原理介紹:類似於 Linq
Elasticsearch 三種分頁方式
data 所有 解決 了解 event 業務層 prism test 不用 from + size
MySQL分頁優化中的“INNER JOIN方式優化分頁算法”到底在什麽情況下會生效?
表結構 files key 效率 ref 兩個 ges 參考 如果 本文出處:http://www.cnblogs.com/wy123/p/7003157.html 最近無意間看到一個MySQL分頁優化的測試案例,並沒有非常具體地說明測試場景的情況下,給出了
Ajax方式分頁加載列表實現
rst join appid subst getx ntp none sta oot 在前面: 最近需要用到這個功能,所以這幾天一直在研究這個,目前大致功能已實現,後續需要完善,但需要的功能點已完成,記錄下; 1、分頁功能引入bootstrap的分頁插件:
SQL分頁查詢的幾種方式
但是 order 結果 htm sql分頁 sele esc 註意 介紹 https://www.cnblogs.com/lxhbky/p/5962393.html 需求:查詢表dbo.Message,每頁10條,查詢第2頁 1:TOP() SELECT TOP(2
django分頁的兩種方式
data number 數據返回 django 當前 star instance previous ref 第一種自定義分頁: def pageDemo(request): ‘‘‘ 自定義分頁] :param request: :return:
分頁儲存管理方式介紹及例題
一、引入 在儲存器管理中連續分配方式會形成許多“碎片”,雖然可以通過“緊湊”方法將許多碎片拼接成可用的大塊空間,但須為之付出很大的開銷,如果一個程序能夠直接分散地裝入到許多不相鄰接的分割槽中,便可充分的利用空間,無需再進行緊湊。基於這一思想便產生了離散分配方式,根據在離散分配時所分配地址空間的基本單位不
也許是 mybaits 史上最簡單的分頁排序方式
mybaits + pageHelper + 自己封裝的分頁 實現 分頁排序 mybaits + pageHelper + 自己封裝的分頁 實現 分頁排序 1 請求示例 1.1 引數說明 1.2
第四章 分頁儲存管理方式
一,儲存管理的離散分配方式 基本分頁儲存管理 基本分段儲存管理 段頁式儲存管理 二,基本分頁儲存管理 離散分配記憶體: 作業規定大小劃分成小份;記憶體也按同樣大小劃分成小份 作業的任一小份可分散放入記憶體任意未使用的小份 1)頁面的概念 記憶體劃分成多個小單元,
SQLserver分頁的四種方式
第一種:ROW_NUMBER() OVER()方式 select * from ( select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels ) as b where RowI
SSM整合兩種配置方式——xml和javaConfig,新增分頁外掛pageHelper和通用Mapper
Spring MVC配置 1. xml方式 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
常見資料庫分頁查詢語句
附:常見資料庫分頁查詢語句 1.oracle資料庫分頁 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=startrow 2.DB2資料庫分頁
前端分頁外掛常見問題總結
關於前端分頁功能實現常見問題分析 2018-10-30 18:23:59 董坤 0 初次使用前端分頁外掛,會出現Cannot read property ‘mData’ of undefined或者 Cannot read property ‘sWidth’
SQL SERVER 分頁的幾種方式。
背景: 在日常工作中,經常需要解決分頁的問題,這也是真分頁必須要做的一件事情。經常查詢,發現有很多的分頁方法,為了瞭解他們的分頁效率我做了一個實驗。當然,跟自己的伺服器效能也是有一定關係的。 準備: 1