參考資料
參數說明
PageInfo{
pageNum=1, //當前頁碼
pageSize=2, //每頁顯示數量
size=2, //當前頁顯示的數量
startRow=1, //開始行
endRow=2, //結束行
total=6, //總記錄數
pages=3, //總頁數
list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=6, pages=3, reasonable=false, pageSizeZero=false},
prePage=0, //上一頁
nextPage=2, //下一頁
isFirstPage=true, //是第一頁
isLastPage=false, // 是最后一頁
hasPreviousPage=false, //有上一頁
hasNextPage=true, //有下一頁
navigatePages=2, //導航頁數
navigateFirstPage=1, //導航到第一頁
navigateLastPage=2, //導航到最后一頁
navigatepageNums=[1, 2] //導航 頁數 頁碼
}
使用步驟
引入依賴
<!--分頁插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
配置文件
#分頁pageHelper
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
代碼示例
Service
public PageInfo<QuestionDTO> list(int pageNo,int size,int pageShowCount) {
PageHelper.startPage(pageNo, size);
List<Question> questionList = questionMapper.list();
PageInfo<Question> questionPageInfo = new PageInfo<>(questionList,pageShowCount);
List<QuestionDTO>questionDTOList = new ArrayList<>();
for(Question question : questionPageInfo.getList()){
User user = userMapper.findById(question.getCreator());
QuestionDTO questionDTO = new QuestionDTO();
BeanUtils.copyProperties(question,questionDTO);
questionDTO.setUser(user);
questionDTOList.add(questionDTO);
}
PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>();
BeanUtils.copyProperties(questionPageInfo, questionDTOPageInfo);
questionDTOPageInfo.setList(questionDTOList);
return questionDTOPageInfo;
}
Controller
@GetMapping("/")
public String hello(HttpServletRequest request, Model model,
@RequestParam(name = "page",defaultValue = "1")Integer pageNo,//當前頁碼
@RequestParam(name = "size",defaultValue = "2")Integer size,
@RequestParam(name = "pageShowCount",defaultValue = "4")Integer pageShowCount
){
Cookie[] cookies = request.getCookies();
/*檢查瀏覽器中有沒有我們設置的cookie對象*/
if(cookies != null){
for(Cookie cookie : cookies){
if( "token".equals(cookie.getName())){
/*根據 cookie 中我們設置的數據來查找數據庫中的用戶信息*/
String token = cookie.getValue();
User user = userMapper.findToken(token);
if(user != null){
/*根據 cookie找到了用戶信息,把它保存到session域中*/
request.getSession().setAttribute("user", user);
}
break;
}
}
}
PageInfo<QuestionDTO> list = questionService.list(pageNo, size, pageShowCount);
model.addAttribute("questions", list);
return "index";
}
前端代碼-分頁條
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous" th:href="@{/(page=${questions.navigateFirstPage})}" >
<span aria-hidden="true">«</span>
</a>
</li>
<li th:each="page:${questions.getNavigatepageNums}" th:class="${questions.pageNum == page}? 'active':'' ">
<a th:href="@{/(page=${page})}" th:text="${page}" ></a>
</li>
<li>
<a href="#" aria-label="Next" th:href="@{/(page=${questions.navigateLastPage})}">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
大坑
在使用pagehelper插件的時候,如果只是對查到的數據進行直接分頁(如下),不會有什么問題,
但是,我們經常需要對查詢的的數據經行二次操作,就如同我的Service層代碼那樣,雖然操作后數據正常分頁,但pageInfo中的參數基本都不是我們想要的,所以需要一些轉變
PageHelper.startPage(pageNo, size);
List<Question> questionList = questionMapper.list();
PageInfo<Question> questionPageInfo = new PageInfo<>(questionList,pageShowCount);