springboot集成mybatis的分页插件pageHelper
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-->