参考
- 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
}
}