Springboot 2.3.5使用 pagehelper 实现分页
参考
- SpringBoot - 获取Get请求参数详解(附样例:非空、默认值、数组、对象)
- 如何使用 PageHelper 一篇博客就够了
- pageHelper详解
- Mybatis中的resultType和resultMap
- 如何使用分页插件
- springboot实现MyBatis分页
- Spring Boot:实现MyBatis分页
- mybatis 中文文档 配置
正文
- pom.xml 节点 dependencies 下添加 pagehelper 依赖
<!-- 分页工具-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
- mapper.xml 添加 id 为 findByPaging 的查(直接查询所有即可,因为在 Service中的
PageHelper.startPage(pageNum, pageSize);
会对这个语句下面的第一个查询进行分页)
<select id="findByPaging" resultType="com.xxx.blog.entity.ArticleEntity" parameterType="map">
select
*
from `articles`
</select>
- ArticleMapper.java
@Mapper
public interface ArticleMapper {
public ArticleEntity findById(Integer id);
public List<ArticleEntity> findByPaging(Map param);
}
- ArticleService.java 新增 findByPaging 方法,将查询出的数据转换为 分页类型,对控制器隐藏处理过程
@Service
public class ArticleService {
@Autowired
ArticleMapper articleMapper;
public ArticleEntity findById(Integer id){
return articleMapper.findById(id);
}
/**
* 分页查询
* @param pageNum
* @param pageSize
* @param map
* @return
*/
public PageInfo findByPaging(Integer pageNum, Integer pageSize,Map map){
PageHelper.startPage(pageNum, pageSize);
List<ArticleEntity> articleEntityPage = articleMapper.findByPaging(map);
PageInfo pageInfo = new PageInfo(articleEntityPage);
return pageInfo;
}
}
- ArticleController.java 下新增 list 方法
@RestController
@RequestMapping("v1/article")
public class ArticleController {
@Autowired
ArticleService articleService;
@GetMapping(value = "", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<PageInfo>> list(@RequestParam(name = "page_num", defaultValue = "1") Integer pageNum, @RequestParam(name = "page_size",defaultValue = "10") Integer pageSize){
Map<String, String> map = new HashMap<>();
PageInfo pageInfo = articleService.findByPaging(pageNum, pageSize, map);
return ResponseEntity.ok(new CustomResponseStructureDto(0, pageInfo, "成功"));
}
@GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> list(@PathVariable("id") Integer id){
ArticleEntity article = articleService.findById(id);
return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, article, "成功"));
}
}
- 测试
localhost:8080/v1/article?page_num=2&page_size=1
{
"code": 0,
"msg": "成功",
"data": {
"total": 2,
"list": [
{
"id": 4,
"title": "测试文章002",
"introduction": "描述内容",
"created_at": null,
"updated_at": null,
"content": "内容12131内容12131内容12131内容12131内容12131内容12131",
"shows": 0,
"likes": 0
}
],
"pageNum": 2,
"pageSize": 1,
"size": 1,
"startRow": 2,
"endRow": 2,
"pages": 2,
"prePage": 1,
"nextPage": 0,
"isFirstPage": false,
"isLastPage": true,
"hasPreviousPage": true,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1,
2
],
"navigateFirstPage": 1,
"navigateLastPage": 2
}
}
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/15136679.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/15136679.html
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义