form1.cn
Make a little progress every day

在Springboot+Mybatis的基础上使用pagehelper实现分页功能

24th of July 2017 Java Java 6104

Spring Boot 集成 MyBatis分页插件 PageHelper, 通用 Mapper,既然人家集成了,那我们肯定要用集成的,当然也可以用独立的pagehelper,本文只讲解集成的方式


1、配置你的pom.xml,增加如下dependency

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>


2、因为Spring boot集成了pagehelper,所以在引入pagehelper依赖后就可以直接使用了


注意:只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页


以下为Service中的方法

@Autowired
private YiArticleMapper yiArticleMapper; //导入mapper

public List<YiArticle> listall(YiArticle yiArticle){
if (yiArticle.getPage() != null && yiArticle.getRows() != null) {
PageHelper.startPage(yiArticle.getPage(), yiArticle.getRows());//直接调用分页类的静态方法
}
return yiArticleMapper.listall();//直接返回全部结果
}


以下为Controller中的方法

@Autowired
private YiArticleService yiArticleService; //导入Service

@GetMapping(value = "/listall")
public ModelAndView listall(YiArticle yiArticle){
ModelAndView result = new ModelAndView("pages");
List<YiArticle> yiArticles = yiArticleService.listall(yiArticle);
result.addObject("pageInfo", new PageInfo<>(yiArticles));//实例化一个PageInfo对象
result.addObject("queryParam", yiArticle);//主要获取?后面参数
result.addObject("page", yiArticle.getPage());
result.addObject("rows", yiArticle.getRows());
return result;
}


以下为模板页代码,本文使用的是thymeleaf

<div>当前页号:<span th:text="${pageInfo.pageNum}"></span></div>
<div>每页条数:<span th:text="${pageInfo.pageSize}"></span></div>
<div>起始行号(>=):<span th:text="${pageInfo.startRow}"></span></div>
<div>终止行号(<=):<span th:text="${pageInfo.endRow}"></span></div>
<div>总结果数:<span th:text="${pageInfo.total}"></span></div>
<div>总页数:<span th:text="${pageInfo.pages}"></span></div>
<hr />
<div>是否为第一页:<span th:text="${pageInfo.isFirstPage}"></span></div>
<div>是否为最后一页:<span th:text="${pageInfo.isLastPage}"></span></div>
<div>是否有前一页:<span th:text="${pageInfo.hasPreviousPage}"></span></div>
<div>是否有下一页:<span th:text="${pageInfo.hasNextPage}"></span></div>
<hr />
<h3>文章列表</h3>
<div>
    <ul>
        <li th:each="prod : ${pageInfo.list}">
            <a href="#" th:text="${prod.title}"></a>
        </li>
    </ul>
</div>
<hr />
<table class="gridtable" style="width:100%;text-align: center;">
    <tr>
        <td th:if="${pageInfo.hasPreviousPage}">
            <a th:href="'?page=1'">首页</a>
        </td>
        <td th:if="${pageInfo.hasPreviousPage}">
            <a th:href="'?page='+${pageInfo.prePage}">上一页</a>
        </td>
        <td th:each="nav : ${pageInfo.navigatepageNums}">
            <a th:href="'?page='+${nav}" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a>
            <span style="font-weight: bold" th:if="${nav == pageInfo.pageNum}" th:text="${nav}"></span>
        </td>
        <td th:if="${pageInfo.hasNextPage}">
            <a th:href="'?page='+${pageInfo.nextPage}">下一页</a>
        </td>
        <td th:if="${pageInfo.hasNextPage}">
            <a th:href="'?page='+${pageInfo.pages}">尾页</a>
        </td>
    </tr>
</table