1.在maven中導入pom依賴
<!-- pageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
其中${pagehelper.version}可以自行選擇當前最高版本
2.配置攔截器插件
<!-- 配置sessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
……………………
……………………
<!--分頁插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
</value>
</property>
</bean>
</array>
</property>
</bean>
在這裏面加一個分頁插件即可。
3.寫一個page工具類,把分頁的一些參數固定在裏面:【根據個人需求可以改動裏面的代碼】
package com.zzx.yamiyami.util;
public class Page {
private int start; //開始頁數
private int count; //每頁顯示個數
private int total; //總數
private String param; //參數
private static final int defaultCount = 10; //默認每頁顯示10條
public Page() {
count = defaultCount;
}
public Page(int start, int count) {
this();
this.start = start;
this.count = count;
}
public boolean isHasPrevious() {
if (start == 0) {
return false;
}
return true;
}
public boolean isHasNext() {
if (start == getLast()) {
return false;
}
return true;
}
public int getTotalPage() {
int totalPage;
if (total % count == 0) {
totalPage = total / count;
} else {
totalPage = total / count + 1;
}
if (totalPage == 0) {
totalPage = 1;
}
return totalPage;
}
public int getLast() {
int last;
if (total % count == 0) {
last = total - count;
} else {
last = total - total % count;
}
last = last < 0 ? 0 : last;
return last;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
@Override
public String toString() {
return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()
+ ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPrevious() + ", isHasNext()="
+ isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";
}
}
4.簡單使用分頁:
【controller中:】
@RequestMapping(value = "chinese")
public String chinese(Model model, Page page){
PageHelper.offsetPage(page.getStart(), page.getCount());
List<Menu> menuList = menuService.findAllMenuByCategoryId(4);
int total = (int) new PageInfo<>(menuList).getTotal();
page.setTotal(total);
model.addAttribute("menuList", menuList);
model.addAttribute("page", page);
return "order/dish";
}
【jsp中】
<c:forEach items="${menuList}" var="menu">
<div>
<tr class="trclass">
<td class="tdone xuhao"><c:out value="${menu.getM_id()}"></c:out></td>
<td class="tdtwo "><c:out value="${menu.getM_name()}"/></td>
<td class="img"><img src="${menu.getM_img()}"></td>
<td class="des"><span><c:out value="${menu.getM_describe()}"/></span></td>
<td class="tdthree"><span class="jiajie"><input type="button" value="-"><span
class="num">0</span><input type="button" value="+"></span></td>
<td class="tdfour"><span>單價:</span><span class="unit"><c:out
value="${menu.getM_price()}"/></span></td>
<td class="tdfive"><span>小計:</span><span class="subtal">0</span>元</td>
</tr>
</div>
</c:forEach>
【在jsp中需要放入頁碼的地方放入】
<div class="pageDiv">
<%@include file="/Page.jsp" %>
</div>
【page.jsp,一個專門用於插入的頁面,只實現page欄】
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<script>
$(function(){
$("ul.pagination li.disabled a").click(function(){
return false;
});
});
</script>
<nav>
<ul class="pagination">
<li <c:if test="${!page.hasPrevious}">class="disabled"</c:if>>
<a href="?start=0${page.param}" aria-label="Previous" >
<span aria-hidden="true">«</span>
</a>
</li>
<li <c:if test="${!page.hasPrevious}">class="disabled"</c:if>>
<a href="?start=${page.start-page.count}${page.param}" aria-label="Previous" >
<span aria-hidden="true">‹</span>
</a>
</li>
<c:forEach begin="0" end="${page.totalPage-1}" varStatus="status">
<li <c:if test="${status.index*page.count==page.start}">class="disabled"</c:if>>
<a
href="?start=${status.index*page.count}${page.param}"
<c:if test="${status.index*page.count==page.start}">class="current"</c:if>
>
${status.count}
</a>
</li>
</c:forEach>
<li <c:if test="${!page.hasNext}">class="disabled"</c:if>>
<a href="?start=${page.start+page.count}${page.param}" aria-label="Next">
<span aria-hidden="true">›</span>
</a>
</li>
<li <c:if test="${!page.hasNext}">class="disabled"</c:if>>
<a href="?start=${page.last}${page.param}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
5.實際效果圖: