1.pom引入依赖
<!--*****************************引入分页插件********************************-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- pagehelper 依赖 -->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>2.1</version>
</dependency>
<!--*****************************引入分页插件********************************-->
2.Java代码中使用
注意配置:application.xml部分
<!-- 配置 MyBatis SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
/**
* 分页展示手游信息列表(条件查询)
* @param pageNo
* @param AppInfo
* @return
*/
@Override
public PageInfo<AppInfo> page(Integer pageNo, AppInfo AppInfo) {
PageHelper.startPage(pageNo, Constants.pageSize," creationDate desc ");
List<AppInfo> li=appInfoMapper.getList(AppInfo);
PageInfo<AppInfo> pageInfo=new PageInfo(li, Constants.pageSize);
return pageInfo;
}
3.前端jsp页面代码
<form action="${ctxf}/comment/scene/page" method="post">
<input type="hidden" name="ticketDate" value="${o.ticketDate }">
<%@ include file="page.jsp" %>
</form>
page.jsp如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<c:if test="${pageInfo.size ==0 }">
<p style="text-align: center;">暂无数据</p>
</c:if>
<c:if test="${pageInfo.size >0 }">
<!-----------------------------------------分页插件-------------------------------------------->
<nav aria-label="Page navigation" id="fenye" style="text-align: center;">
<input type="hidden" value="${pageNo }" name="pageNo" id="pageNo"/>
<button type="submit" style="display: none" id="sub"></button>
<ul class="pagination">
<!-----------------------------------------首页分割线-------------------------------------------->
<li class="page-item"><a class="page-link" data-p=1
href="javascript:;">首页</a></li>
<!-----------------------------------------上一页分割线------------------------------------------>
<c:if test="${pageInfo.hasPreviousPage && pageInfo.pageNum>1}">
<li class="page-item"><a class="page-link"
href="javascript:;" data-p=${pageInfo.pageNum-1}
aria-label="Previous"> <span aria-hidden="true">«</span>
</a></li>
</c:if>
<!-----------------------------------------数字翻页分割线---------------------------------------->
<c:if test="${pageInfo.pages<=10}">
<c:forEach begin="1" end="${pageInfo.pages }" var="i">
<c:choose>
<c:when test="${i eq pageInfo.pageNum }">
<li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:when>
<c:otherwise>
<li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:if>
<c:if test="${pageInfo.pages>10}">
<c:if test="${pageInfo.pageNum-5>0}">
<c:if test="${pageInfo.pageNum+5>=pageInfo.pages}">
<c:forEach begin="${pageInfo.pages-9 }" end="${pageInfo.pages}" var="i">
<c:choose>
<c:when test="${i eq pageInfo.pageNum }">
<li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:when>
<c:otherwise>
<li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:if>
<c:if test="${pageInfo.pageNum+5<pageInfo.pages}">
<c:forEach begin="${pageInfo.pageNum-5 }" end="${pageInfo.pageNum+4}"
var="i">
<c:choose>
<c:when test="${i eq pageInfo.pageNum }">
<li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:when>
<c:otherwise>
<li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:if>
</c:if>
<c:if test="${pageInfo.pageNum-5<=0}">
<c:forEach begin="1" end="10" var="i">
<c:choose>
<c:when test="${i eq pageInfo.pageNum }">
<li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:when>
<c:otherwise>
<li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
</c:if>
</c:if>
<!-----------------------------------------下一页分割线------------------------------------------>
<c:if test="${pageInfo.pageNum>1&&pageInfo.pageNum+1<pageInfo.pages}">
<li class="page-item"><a class="page-link"
href="javascript:;" data-p=${pageInfo.pageNum+1}
aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</c:if>
<!-----------------------------------------末页分割线-------------------------------------------->
<li class="page-item"><a class="page-link"
href="javascript:;" data-p=${pageInfo.pages}>末页</a></li>
</ul>
</nav>
<p style="text-align:center;clear: both;">共${pageInfo.total }条数据,当前是第${pageInfo.pageNum }页,每页${pageInfo.pageSize }条,共${pageInfo.pages }页</p>
</c:if>
<script type="text/javascript">
$("#fenye a").click(function(){
$("#pageNo").val( $(this).attr("data-p"));
$("#sub").click();
return false;
})
</script>