1.pom引入依赖

  1. <!--*****************************引入分页插件********************************-->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper</artifactId>
  5. <version>5.1.10</version>
  6. </dependency>
  7. <!-- pagehelper 依赖 -->
  8. <dependency>
  9. <groupId>com.github.jsqlparser</groupId>
  10. <artifactId>jsqlparser</artifactId>
  11. <version>2.1</version>
  12. </dependency>
  13. <!--*****************************引入分页插件********************************-->

2.Java代码中使用

注意配置:application.xml部分

  1. <!-- 配置 MyBatis SqlSessionFactoryBean-->
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3. <property name="dataSource" ref="dataSource"/>
  4. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  5. <!-- 配置分页插件 -->
  6. <property name="plugins">
  7. <array>
  8. <bean class="com.github.pagehelper.PageInterceptor">
  9. <property name="properties">
  10. <!--使用下面的方式配置参数,一行配置一个 -->
  11. <value>
  12. helperDialect=mysql
  13. </value>
  14. </property>
  15. </bean>
  16. </array>
  17. </property>
  18. </bean>
  1. /**
  2. * 分页展示手游信息列表(条件查询)
  3. * @param pageNo
  4. * @param AppInfo
  5. * @return
  6. */
  7. @Override
  8. public PageInfo<AppInfo> page(Integer pageNo, AppInfo AppInfo) {
  9. PageHelper.startPage(pageNo, Constants.pageSize," creationDate desc ");
  10. List<AppInfo> li=appInfoMapper.getList(AppInfo);
  11. PageInfo<AppInfo> pageInfo=new PageInfo(li, Constants.pageSize);
  12. return pageInfo;
  13. }

3.前端jsp页面代码

  1. <form action="${ctxf}/comment/scene/page" method="post">
  2. <input type="hidden" name="ticketDate" value="${o.ticketDate }">
  3. <%@ include file="page.jsp" %>
  4. </form>

page.jsp如下:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <c:if test="${pageInfo.size ==0 }">
  4. <p style="text-align: center;">暂无数据</p>
  5. </c:if>
  6. <c:if test="${pageInfo.size >0 }">
  7. <!-----------------------------------------分页插件-------------------------------------------->
  8. <nav aria-label="Page navigation" id="fenye" style="text-align: center;">
  9. <input type="hidden" value="${pageNo }" name="pageNo" id="pageNo"/>
  10. <button type="submit" style="display: none" id="sub"></button>
  11. <ul class="pagination">
  12. <!-----------------------------------------首页分割线-------------------------------------------->
  13. <li class="page-item"><a class="page-link" data-p=1
  14. href="javascript:;">首页</a></li>
  15. <!-----------------------------------------上一页分割线------------------------------------------>
  16. <c:if test="${pageInfo.hasPreviousPage && pageInfo.pageNum>1}">
  17. <li class="page-item"><a class="page-link"
  18. href="javascript:;" data-p=${pageInfo.pageNum-1}
  19. aria-label="Previous"> <span aria-hidden="true">&laquo;</span>
  20. </a></li>
  21. </c:if>
  22. <!-----------------------------------------数字翻页分割线---------------------------------------->
  23. <c:if test="${pageInfo.pages<=10}">
  24. <c:forEach begin="1" end="${pageInfo.pages }" var="i">
  25. <c:choose>
  26. <c:when test="${i eq pageInfo.pageNum }">
  27. <li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  28. </c:when>
  29. <c:otherwise>
  30. <li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  31. </c:otherwise>
  32. </c:choose>
  33. </c:forEach>
  34. </c:if>
  35. <c:if test="${pageInfo.pages>10}">
  36. <c:if test="${pageInfo.pageNum-5>0}">
  37. <c:if test="${pageInfo.pageNum+5>=pageInfo.pages}">
  38. <c:forEach begin="${pageInfo.pages-9 }" end="${pageInfo.pages}" var="i">
  39. <c:choose>
  40. <c:when test="${i eq pageInfo.pageNum }">
  41. <li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  42. </c:when>
  43. <c:otherwise>
  44. <li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  45. </c:otherwise>
  46. </c:choose>
  47. </c:forEach>
  48. </c:if>
  49. <c:if test="${pageInfo.pageNum+5<pageInfo.pages}">
  50. <c:forEach begin="${pageInfo.pageNum-5 }" end="${pageInfo.pageNum+4}"
  51. var="i">
  52. <c:choose>
  53. <c:when test="${i eq pageInfo.pageNum }">
  54. <li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  55. </c:when>
  56. <c:otherwise>
  57. <li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  58. </c:otherwise>
  59. </c:choose>
  60. </c:forEach>
  61. </c:if>
  62. </c:if>
  63. <c:if test="${pageInfo.pageNum-5<=0}">
  64. <c:forEach begin="1" end="10" var="i">
  65. <c:choose>
  66. <c:when test="${i eq pageInfo.pageNum }">
  67. <li class=" page-item active"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  68. </c:when>
  69. <c:otherwise>
  70. <li class=" page-item"><a href="javascript:;" class="page-link" data-p=${i}>${i}</a></li>
  71. </c:otherwise>
  72. </c:choose>
  73. </c:forEach>
  74. </c:if>
  75. </c:if>
  76. <!-----------------------------------------下一页分割线------------------------------------------>
  77. <c:if test="${pageInfo.pageNum>1&&pageInfo.pageNum+1<pageInfo.pages}">
  78. <li class="page-item"><a class="page-link"
  79. href="javascript:;" data-p=${pageInfo.pageNum+1}
  80. aria-label="Next"> <span aria-hidden="true">&raquo;</span>
  81. </a></li>
  82. </c:if>
  83. <!-----------------------------------------末页分割线-------------------------------------------->
  84. <li class="page-item"><a class="page-link"
  85. href="javascript:;" data-p=${pageInfo.pages}>末页</a></li>
  86. </ul>
  87. </nav>
  88. <p style="text-align:center;clear: both;">共${pageInfo.total }条数据,当前是第${pageInfo.pageNum }页,每页${pageInfo.pageSize }条,共${pageInfo.pages }页</p>
  89. </c:if>
  90. <script type="text/javascript">
  91. $("#fenye a").click(function(){
  92. $("#pageNo").val( $(this).attr("data-p"));
  93. $("#sub").click();
  94. return false;
  95. })
  96. </script>