SSM project forms a paging operation (PageHepler use)

Maven pom.xml add dependencies

<dependency>
<the groupId> com.github.pagehelper </ the groupId>
<the artifactId> pagehelper </ the artifactId>
<Version> 5.1.2 </ Version>
</ dependency>
. 1
2
. 3
. 4
. 5
Add an interceptor in the applicationContext.xml SqlSessionFactoryBean database can be replaced I use the mysql database, other databases to mysql database can be replaced with another.

<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 传入PageHelper的插件 -->
<property name="plugins">
<array>
<!-- 传入插件的对象 -->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
. 17
below the tab can be used, the need to call one line of code is added to the dao layer ServiceImpl
PageHelper.startPage (pageNum, the pageSize);

@Override
public List <the Syslog> selall (pageNum Integer, Integer the pageSize) {
PageHelper.startPage (pageNum, the pageSize);
return iSysLogDao.selAll ();
}
. 1
2
. 3
. 4
. 5
Controller layer needs to store information on this subject PageInfo

@RequestMapping("/show")
public ModelAndView show(@RequestParam(defaultValue = "1",required = true) Integer pageNum,@RequestParam(defaultValue = "10",required = true) Integer pageSize){
ModelAndView modelAndView=new ModelAndView("syslog-list");
List<Syslog> syslogList=iSysLogService.selAll(pageNum,pageSize);
PageInfo pageInfo=new PageInfo(syslogList);
modelAndView.addObject("pageInfo",pageInfo);

ModelAndView return;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
Note that the front end call pageInfo.list

<C: forEach items = "$ {} pageInfo.list" var = "the syslog">
<TR>
<TD> <INPUT name = "IDS" type = "CheckBox"> </ TD>
<TD> {$ the syslog. } ID </ TD>
<TD> syslog.visitTimeStr $ {} </ TD>
<TD> syslog.username $ {} </ TD>
<TD> syslog.ip $ {} </ TD>
<TD> $ { } syslog.url </ TD>
<TD> $ {ms} syslog.executionTime </ TD>
<TD> syslog.method $ {} </ TD>
</ TR>
</ C: forEach>
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
PageInfo encapsulates paging information can be used directly,

<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/sysLog/show?pageNum=1&pageSize=${pageInfo.pageSize}" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/sysLog/show?pageNum=${pageInfo.pageNum-1}&pageSize=${pageInfo.pageSize}">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/sysLog/show?pageNum=${pageNum}&pageSize=${pageInfo.pageSize}">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/sysLog/show?pageNum=${pageInfo.pageNum+1}&pageSize=${pageInfo.pageSize}">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/sysLog/show?pageNum=${pageInfo.pages}&pageSize=${pageInfo.pageSize}" aria-label="Next">尾页</a></li>
</ul>
</div>
1
2
3
4
5
6
7
8
9
10
11
下面是PageInfo源代码

/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.github.pagehelper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/ **
* on Page <E> results Packaging
* <the p-/>
* Add a tab of a number of properties, mainly reference: HTTP: //bbs.csdn.net/topics/360010907
*
* @author liuzh / abel533 / isea533
* @version 3.3.0
* 3.2.2 @Since
* LOCATION: http://git.oschina.net/free/Mybatis_PageHelper
* /
@SuppressWarnings ({ "rawtypes", "an unchecked"})
public class PageInfo <T > the implements the Serializable {
Private Long Final static serialVersionUID = 1L;
// this page
Private int pageNum;
// number per
Private int the pageSize;
// number of this page
private int size;

// Since startRow and endRow not common here that a specific usage
// can "show startRow to endRow total size of data" in the page

// current page is the first element of the row number in the database of
private int startRow;
// current page last element of the line number in the database of
private int endRow;
// total number of records
private Total Long;
// total number of pages
private Pages int;
// result set
private List <T> list;

// a former
Private int prePage;
// Next
private int nextPage;

// whether the first page of the
Private isFirstPage boolean = false;
// whether the last page of the
Private isLastPage boolean = false;
// Are there previous page
Private hasPreviousPage boolean = false;
// if there is a next
private boolean hasNextPage = to false;
// navigation page number
Private int navigatePages;
// all navigation page number
private int [] navigatepageNums;
a first page on the navigation bar //
private int navigateFirstPage;
the last page on the navigation bar //
private int navigateLastPage;

public PageInfo() {
}

/**
* 包装Page对象
*
* @param list
*/
public PageInfo(List<T> list) {
this(list, 8);
}

/**
* 包装Page对象
*
* @param list page结果
* @param navigatePages 页码数量
*/
public PageInfo(List<T> list, int navigatePages) {
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();

= page.getPages this.pages ();
This.List = Page;
this.size page.size = ();
this.total page.getTotal = ();
// since the result is> startRow, the actual needs + . 1
IF (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} the else {
this.startRow page.getStartRow = () +. 1;
// calculate the actual endRow (a last time special)
this.endRow this.startRow = - + this.size. 1;
}
} the else IF (Collection List the instanceof) {
this.pageNum =. 1;
this.pageSize list.size = ();

= this.pageSize this.pages> 0. 1: 0;?
This.List = List;
this.size list.size = ();
this.total list.size = ();
this.startRow = 0;
this.endRow = ()> 0 list.size () list.size -. 1:? 0;
}
IF (Collection List the instanceof) {
this.navigatePages = navigatePages;
// calculate navigation page
calcNavigatepageNums ();
// calculate the front pages, the first page Finally, a
calcPage ();
// Analyzing page boundaries
judgePageBoudary ();
}
}

/ **
* Calculation navigation page
* /
Private void calcNavigatepageNums () {
// when the total number of pages is less than or equal to the number of pages navigation
IF (Pages <= navigatePages) {
navigatepageNums = new new int [Pages];
for (int I = 0 ; I <pages; I ++) {
navigatepageNums [I] = I +. 1;
}
when} else {// when the total number of pages is larger than the page navigation
navigatepageNums = new new int [navigatePages];
int = pageNum StartNum - navigatePages / 2;
int endNum = pageNum + navigatePages / 2;

if (startNum < 1) {
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
} else if (endNum > pages) {
endNum = pages;
//最后navigatePages页
for (int i = navigatePages - 1; i >= 0; i--) {
navigatepageNums[i] = endNum--;
}
} else {
//所有中间页
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
}
}
}

/**
* 计算前后页,第一页,最后一页
*/
private void calcPage() {
if (navigatepageNums != null && navigatepageNums.length > 0) {
navigateFirstPage = navigatepageNums[0];
navigateLastPage = navigatepageNums[navigatepageNums.length - 1];
if (pageNum > 1) {
prePage = pageNum - 1;
}
if (pageNum < pages) {
nextPage = pageNum + 1;
}
}
}

/ **
*判定页面边界
* /
private void judgePageBoudary () {
isFirstPage pageno = == 1;
islastpage pageno = == page || pages == 0 ;;
hasPreviousPage pageno => 1;
hasNextPage pageno = <pages;
}

getPageNum public int () {
return pageno;
}

public void setPageNum (int pageno) {
this.pageNum = pageno;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public int getStartRow() {
return startRow;
}

public void setStartRow(int startRow) {
this.startRow = startRow;
}

public int getEndRow() {
return endRow;
}

public void setEndRow(int endRow) {
this.endRow = endRow;
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}

public int getPages() {
return pages;
}

public void setPages(int pages) {
this.pages = pages;
}

public List<T> getList() {
return list;
}

public void setList(List<T> list) {
this.list = list;
}

The @deprecated
// firstPage is 1, this function is the first page acquired in the navigation bar, ambiguous and
public int getFirstPage () {
return navigateFirstPage;
}

@Deprecated
public void setFirstPage(int firstPage) {
this.navigateFirstPage = firstPage;
}

public int getPrePage() {
return prePage;
}

public void setPrePage(int prePage) {
this.prePage = prePage;
}

public int getNextPage() {
return nextPage;
}

public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}

The @deprecated
// Please GetPages () to get the last one, the function is the last one acquired on the navigation bar, ambiguous and.
Public int getLastPage () {
return navigateLastPage;
}

@Deprecated
public void setLastPage(int lastPage) {
this.navigateLastPage = lastPage;
}

public boolean isIsFirstPage() {
return isFirstPage;
}

public void setIsFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}

public boolean isIsLastPage() {
return isLastPage;
}

public void setIsLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}

public boolean isHasPreviousPage() {
return hasPreviousPage;
}

public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}

public boolean isHasNextPage() {
return hasNextPage;
}

public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}

public int getNavigatePages() {
return navigatePages;
}

public void setNavigatePages(int navigatePages) {
this.navigatePages = navigatePages;
}

public int[] getNavigatepageNums() {
return navigatepageNums;
}

public void setNavigatepageNums(int[] navigatepageNums) {
this.navigatepageNums = navigatepageNums;
}

public int getNavigateFirstPage() {
return navigateFirstPage;
}

public int getNavigateLastPage() {
return navigateLastPage;
}

public void setNavigateFirstPage(int navigateFirstPage) {
this.navigateFirstPage = navigateFirstPage;
}

public void setNavigateLastPage(int navigateLastPage) {
this.navigateLastPage = navigateLastPage;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("PageInfo{");
sb.append("pageNum=").append(pageNum);
sb.append(", pageSize=").append(pageSize);
sb.append(", size=").append(size);
sb.append(", startRow=").append(startRow);
sb.append(", endRow=").append(endRow);
sb.append(", total=").append(total);
sb.append(", pages=").append(pages);
sb.append(", list=").append(list);
sb.append(", prePage=").append(prePage);
sb.append(", nextPage=").append(nextPage);
sb.append(", isFirstPage=").append(isFirstPage);
sb.append(", isLastPage=").append(isLastPage);
sb.append(", hasPreviousPage=").append(hasPreviousPage);
sb.append(", hasNextPage=").append(hasNextPage);
sb.append(", navigatePages=").append(navigatePages);
sb.append(", navigateFirstPage=").append(navigateFirstPage);
sb.append(", navigateLastPage=").append(navigateLastPage);
sb.append(", navigatepageNums=");
if (navigatepageNums == null) sb.append("null");
else {
sb.append('[');
for (int i = 0; i < navigatepageNums.length; ++i)
sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]);
sb.append(']');
}
sb.append('}');
return sb.toString();
}
}

--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10995940.html