引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
application.properties配置
# pagehelper分页插件配置
pagehelper.helper-dialect=mysql
pagehelper.support-methods-arguments=true
Controller层
@GetMapping("list")
public ApiResult<PagedData> getSiteListNotDeleteByTagId(@RequestParam("tagId") Integer tagId,
@RequestParam("pageNum") Integer pageNum,
@RequestParam("pageSize") Integer pageSize) {
PagedData pagedData = siteService.getSiteListNotDeleteByTagId(tagId, pageNum, pageSize);
return ApiResult.success(pagedData);
}
Service层
@Override
public PagedData getSiteListNotDeleteByTagId(Integer tagId, Integer pageNum, Integer pageSize) {
Example example = new Example(Site.class);
criteria.andEqualTo("tagid", tagId);
/*第一步:PageHelper.startPage必须放在最前面,只针对最近的一次查询*/
PageHelper.startPage(pageNum, pageSize);
/*第二步:获取数据*/
List<Site> siteList = siteMapper.selectByExample(example);
/*第三步:返回封装的分页数据*/
return PageUtil.getPagedData(siteList);
}
分页数据工具类
public class PageUtil {
public static PagedData getPagedData(List<?> list) {
PageInfo<?> pageInfo = new PageInfo<>(list);
PagedData pageData = new PagedData();
pageData.setPageNum(pageInfo.getPageNum());
pageData.setPageSize(pageInfo.getPageSize());
pageData.setPages(pageInfo.getPages());
pageData.setTotal(pageInfo.getTotal());
pageData.setPageList(pageInfo.getList());
return pageData;
}
}