本篇是基于spring boot 1.5.6.RELEASE,如果按照以下步骤不能实现分页,那可能是pagehelper的版本问题。更换版本试一下。
整合过程
pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
application.properties
注意:
本人试了一下,这些属性可以不加,依然可以实现分页功能。先记到这里。
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
ApplicationsController.java
import com.github.pagehelper.PageInfo;
import com.ruubypay.miss.oauthcenter.modules.applications.dao.entity.Applications;
import com.ruubypay.miss.oauthcenter.modules.applications.service.ApplicationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author: wangsaichao
* @date: 2018/4/26
* @description: 应用 controller
*/
@Controller
@RequestMapping("/applications")
public class ApplicationsController {
@Autowired
private ApplicationsService applicationsService;
/**
* 跳转到应用列表页面
* @param pageNo 要显示第几页内容
* @param pageSize 一页显示多少条
* @param model
* @return
*/
@RequestMapping("/toApplicationList.html")
public String toApplicationList(@RequestParam(value="pageNo",defaultValue="1")int pageNo, @RequestParam(value="pageSize",defaultValue="10")int pageSize, Model model){
PageInfo<Applications> page = applicationsService.getApplicationList(pageNo,pageSize);
model.addAttribute("pageInfo", page);
return "backend/applications/application_list";
}
}
ApplicationsServiceImpl.java
注意:只有紧跟在 PageHelper.startPage 方法后的第一个 MyBatis 的查询(select)方法会被分页
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruubypay.miss.oauthcenter.modules.applications.dao.ApplicationsMapper;
import com.ruubypay.miss.oauthcenter.modules.applications.dao.entity.Applications;
import com.ruubypay.miss.oauthcenter.modules.applications.service.ApplicationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: wangsaichao
* @date: 2018/4/28
* @description: 应用列表service层
*/
@Service
public class ApplicationsServiceImpl implements ApplicationsService {
@Autowired
private ApplicationsMapper applicationsMapper;
/**
* 查询所有的应用
* @param pageNo
* @param pageSize
* @return
*/
@Override
public PageInfo<Applications> getApplicationList(int pageNo, int pageSize) {
PageHelper.startPage(pageNo,pageSize);
List<Applications> list = applicationsMapper.getApplicationPage();
//用PageInfo对结果进行包装
PageInfo<Applications> page = new PageInfo<Applications>(list);
return page;
}
}
ApplicationsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ruubypay.miss.oauthcenter.modules.applications.dao.ApplicationsMapper">
<!-- 查询所有的应用 -->
<select id="getApplicationPage" resultType="com.ruubypay.miss.oauthcenter.modules.applications.dao.entity.Applications">
select * from applications
</select>
</mapper>
thmeleaf页面
页面是自己随便写的一个垃圾页面,只是想展示一下pageInfo的属性
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>如意行开放平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link th:href="@{/css/style.css}" rel="stylesheet"/>
</head>
<body>
<div style="text-align: center;margin:0 auto;width: 1000px; ">
<h1>如意行开放平台</h1>
<a href="/testjsp/index">云平台首页</a>
<a href="/document/center">文档中心</a>
</div>
<div style="height: 15px;"><hr/></div>
<div style="float:left;width:10%;height:800px;border:1px">
<h3 style="text-align:left">用户中心</h3>
<a href="/cooperation/toCooperationCenter">个人信息</a><br/>
<a href="/cooperation/toApplicationList.html">我的应用</a><br/>
</div>
<div style="float:left;width:1%;height:800px;border:1px">
<hr style="width:1px;height:800px; "/>
</div>
<div style="float:left;width:80%;height:800px;border:1px">
<div class="container" style="margin:0 auto;width:1000px;">
</div>
<h3 style="width:400px;text-align:center">我的应用</h3>
<hr/>
<table class="tablelist">
<thead>
<tr align="left" class= "d">
<th width="5%">序号</th>
<th width="10%">应用名称</th>
<th width="15%">APPID</th>
<th width="10%">状态</th>
<th width="10%">操作</th>
</tr>
</thead>
<tbody>
<tr th:each="application : ${pageInfo.list}">
<td th:text="${application.id}"></td>
<td th:text="${application.app_name}"></td>
<td th:text="${application.app_id}"></td>
<td th:text="${application.app_status}"></td>
<td>
<a href="#" onclick="detail(${obj.id});">详情</a>
</td>
</tr>
</tbody>
</table>
<hr />
<table class="gridtable" style="width:100%;text-align: center;">
<tr>
<td width="10%" th:if="${pageInfo.hasPreviousPage}">
<a th:href="'?pageNo=1'">首页</a>
</td>
<td width="10%" th:if="${pageInfo.hasPreviousPage}">
<a th:href="'?pageNo='+${pageInfo.prePage}">上一页</a>
</td>
<td width="10%" th:each="nav : ${pageInfo.navigatepageNums}">
<a th:href="'?pageNo='+${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 width="10%" th:if="${pageInfo.hasNextPage}">
<a th:href="'?pageNo='+${pageInfo.nextPage}">下一页</a>
</td>
<td width="10%" th:if="${pageInfo.hasNextPage}">
<a th:href="'?pageNo='+${pageInfo.pages}">尾页</a>
</td>
</tr>
</table>
<hr />
<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>
</div>
</body>
</html>
页面如下: