Pagehelper 使用笔记
参考资料
参数说明
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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!