1、添加分頁插件依賴文件
<properties> <java.version>1.7</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- SpringBoot 集成mybatis相關的依賴包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 分頁插件pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-autoconfigure</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- 分頁插件pagehelper --> </dependencies>
2、配置application.yml文件
spring: mvc: view: prefix: / suffix: .jsp datasource: url: jdbc:mysql://localhost:3306/myrec?characterEncoding=utf8&useSSL=true username: root password: m123456 driver-class-name: com.mysql.jdbc.Driver #配置分頁插件pagehelper pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
3、控制器層的使用
@Autowired PostInfoService postInfo; @RequestMapping("/info") public String getAll(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model){ //獲取第1頁,5條內容,默認查詢總數count /* 第一個參數是第幾頁;第二個參數是每頁顯示條數 */ PageHelper.startPage(pn, 3); List<PostInfor> postIn = postInfo.getAll(); System.out.println(postIn+"==========="); //用PageInfo對結果進行包裝 PageInfo<PostInfor> page = new PageInfo<PostInfor>(postIn); model.addAttribute("pageInfo", page); return "index"; }
4、index頁面-分頁部分
<!-- 分頁 --> <div class="ui circular labels" style="float: right;"> <a class="ui label">當前第 ${pageInfo.pageNum }頁,總${pageInfo.pages } 頁,總 ${pageInfo.total } 條記錄</a> <a class="ui label" href="info?pn=1">首頁</a> <c:if test="${pageInfo.hasPreviousPage }"> <a class="ui label" href="info?pn=${pageInfo.pageNum-1 }">«</a> </c:if> <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num"> <c:if test="${page_Num == pageInfo.pageNum }"> <a class="ui label" href="info?pn=${page_Num}">${page_Num}</a> </c:if> <c:if test="${page_Num != pageInfo.pageNum }"> <a class="ui label" href="info?pn=${page_Num}">${page_Num }</a> </c:if> </c:forEach> <c:if test="${pageInfo.hasNextPage }"> <a class="ui label" href="info?pn=${pageInfo.pageNum+1 }">»</a> </c:if> <a class="ui label" href="info?pn=${pageInfo.pages}">末頁</a> </div> <!-- 分頁 end-->