1.物理分頁
物理分頁依賴的是某一物理實體,這個物理實體就是數據庫,好比MySQL數據庫提供了limit關鍵字,程序員只須要編寫帶有limit關鍵字的SQL語句,數據庫返回的就是分頁結果。建議使用。html
2.邏輯分頁
邏輯分頁依賴的是程序員編寫的代碼。數據庫返回的不是分頁結果,而是所有數據,而後再由程序員經過代碼獲取分頁數據,經常使用的操做是一次性從數據庫中查詢出所有數據並存儲到List集合中,由於List集合有序,再根據索引獲取指定範圍的數據。java
MyBatis 分頁插件 - PageHelper
該插件目前支持如下數據庫的物理分頁:mysql
Oracle
Mysql
MariaDB
SQLite
Hsqldb
PostgreSQL
DB2
SqlServer(2005,2008)
Informix
H2
SqlServer2012
Derby
Phoenix
分頁插件 5.0
因爲分頁插件 5.0 版本和 4.2.x 實現徹底不一樣,因此 master 分支爲 5.x 版本,4.2 做爲一個分支存在,若是有針對 4.2 的 PR,請注意提交到分支版本。git
集成
使用 PageHelper 你只須要在 classpath 中包含 pagehelper-x.x.x.jar 和 jsqlparser-0.9.5.jar。程序員
若是你使用 Maven,你只須要在 pom.xml 中添加下面的依賴:github
<dependency>
<groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本</version> </dependency>
本次開發環境:JDK1.8+MySql5.0+Mybatis-pagehelper4.2.1+mavensql
注意maven中的其餘依賴的版本可能會影響該插件,致使引入失敗數據庫
使用步驟mybatis
1.在maven pom.xml中添加依賴
2.在Mybatis-config.xml中sqlSessionFactory中添加插件配置
<!--分頁插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
</value>
</property>
</bean>
</array>
</property>
3.寫sql、service
public PageInfo<Student> selectByUnitId(Integer id,Integer currPage,Integer pageSize) {
PageHelper.startPage(currPage,pageSize);//當前頁面編號+(從第0頁開始),每頁的大小
PageInfo<Student> pageInfo = new PageInfo<Student>(studentDao.selectByUnitId(id));
return pageInfo;
}
建議sql不要這樣簡單粗暴的查詢
sql:select * from student;
即便如此pageHelper插件也會自動的查詢指定的條數
PageInfo:大體包含如下信息:{pageNum=1, pageSize=5, size=5, startRow=1, endRow=5, total=10, pages=2, list{...}}
每次查詢以後將指定條數的數據放在PageInfo中。
在Controller層 把pageInfo放在requestScope中,命名student(與下文同)
這樣就能夠根據pageNum輸出指定的信息
jsp:分頁標籤
這是一個完整的分頁標籤,只要更改參數便可 <div class="text-center">
<nav>
<ul class="pagination">
<li>
<a href="<c:url value="/student?unitId=${param.unitId}&currPage=1&unitName=${param.unitName}"/>">首頁</a>
</li>
<li>
<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pageNum-1>1?student.pageNum-1:1}&unitName=${param.unitName} "/>">«</a>
</li>
<c:forEach begin="1" end="${student.pages}" varStatus="loop">
<c:set var="active" value="${loop.index==student.pageNum?'active':''}"/>
<li class="${active}"><a href="<c:url value="/student?unitId=${param.unitId}&currPage=${loop.index}&unitName=${param.unitName}"/>">${loop.index}</a>
</li>
</c:forEach>
<li>
<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pageNum+1<student.pages?student.pageNum+1:student.pages}&unitName=${param.unitName}"/>">»</a>
</li>
<li>
<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pages}&unitName=${param.unitName}"/>">尾頁</a>
</li>
</ul>
</nav>
</div>
js:分頁代碼
function createPaginationNav(e, t, a, n, p, o, i, s) {
null == e && (e = ""), e = e.replace(/\&currPage=\d+\&/, "&"), e = e.replace(/\&?currPage=\d+\&?/, "");
var r = e.length;
r > 0 && "?" == e.charAt(r - 1) && (e = e.replace("?", "")), null == i && (i = ""), "undefined" == typeof s && (s = 10);
var g = s,
l = e,
f = l.indexOf("?");
if (l += f > 0 ? "&currPage=" : "?currPage =", document.write('<span style="font-size:12px;">第' + t + "頁 </span>"), document.write('<span style="font-size:12px;">共' + p + "頁 </span>"), p > 1) {
1 == t ? (document.write('<a href="#">首頁</a>'), document.write('<a href="#" title="上一頁"><span style="font-size:12px;"><<</span></a>')) : (document.write('<a href="' + l + '1">首頁</a>'), document.write('<a href="' + l + a + '" title="上一頁"><span style="font-size:12px;"><<</span></a>'));
var d = 1;
if (p > g) {
var u = 0,
c = 0,
m = Math.round(g / 2);
for (d = p / g + 1, t > m && p - m >= t ? (u = t - m, c = t + m - 1) : m >= t ? (u = 1, c = g) : (u = p - g + 1, c = p), c > p && (c = p), ipage = u; c >= ipage; ipage++) p >= ipage && document.write(t == ipage ? '<a href="' + l + ipage + '" ><span style="font-size:18px;color:red;">' + ipage + "</span></a>" : '<a href="' + l + ipage + '" ><span style="font-size:12px;">' + ipage + "</span></a>")
} else
for (ipage = 1; p >= ipage; ipage++) document.write(t == ipage ? '<a href="' + l + ipage + '" ><span style="font-size:18px;color:red;">' + ipage + "</span></a>" : '<a href="' + l + ipage + '" ><span style="font-size:12px;">' + ipage + "</span></a>");
t == p ? (document.write('<a href="#" title="下一頁"><span style="font-size:12px;">>></span></a>'), document.write('<a href="#">尾頁</a>')) : (document.write('<a href="' + l + n + '" title="下一頁"><span style="font-size:12px;">>></span></a>'), document.write('<a href="' + l + p + '">尾頁</a>')), document.write('<input type="text" id="gotopage' + i + '" size="2" onkeypress="if (event.keyCode == 13)goto_page(\'' + l + "','" + i + '\')"> <a href="#" onclick="goto_page(\'' + l + "','" + i + '\')"><span style="font-size:12px;">GOTO</span></a>')
}
}
function goto_page(e, t) {
var a = /^[0-9]+$/;
return gotoInputId = "gotopage" + t, a.test(document.getElementById(gotoInputId).value) ? void(location.href = e + document.getElementById(gotoInputId).value) : (alert("請輸入數字!"), document.getElementById(gotoInputId).value = "", !1)
}
//在jsp中調用此函數
<div class="page right">
<script language="javascript">
createPaginationNav('${pageContext.request.contextPath}/student?unitId=${param.unitId}&currPage=0',
${student.pageNum},
${student.prePage},
${student.nextPage},
${student.pages},
${student.pageSize},
'');
</script>
</div>
MyBatis plus通用Mapper實現分頁
安裝:http://www.javashuo.com/article/p-vhhbfmcg-ba.html
邏輯分頁
private ApplicationContext ioc =
new ClassPathXmlApplicationContext("applicationContext.xml"); private EmployeeMapper employeeMapper= ioc.getBean("employeeMapper", EmployeeMapper.class); //5. 分頁查詢page集成rowBounds Page(currPage,Pagesize);(內存分頁)
List<Employee> emps = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println(emps);
使用Mybatisplus插件中的PageIntercepter插件實現物理分頁
<property name="plugins"> <!--分頁插件註冊--> <list> <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></bean> </list> </property>
//測試分頁
public void testPaginationInterceptor(){
IPage<Employee> pages =employeeMapper.selectPage(new Page<>(1,5),null);//便可實現分頁
System.out.println(pages.getRecords());
}
sql執行:說明這是一個實實在在的物理分頁
DEBUG 10-02 11:37:07,239 ==> Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee LIMIT 0,5
IPage對象的方法:
default IPage<T> setPages(long pages) {
return this;
}
List<T> getRecords();
IPage<T> setRecords(List<T> var1);
long getTotal();
IPage<T> setTotal(long var1);
long getSize();
IPage<T> setSize(long var1);
long getCurrent();
IPage<T> setCurrent(long var1);