1. boot的分页查询
使用PageHelper的starter
-
在pom.xml中引入依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.3</version> </dependency>
-
在application.properties或者application.yml格式配置pagehelper的属性
#pagehelper分页插件配置 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql
#分页配置 pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql
-
使用 :最后就是使用pagehelper进行分页了,其中最重要的一句就是PageHelper.startPage(pageNum,pageSize);pageNum:当前页数 pageSize:当前页需要显示的数量
@GetMapping("/selectPage") @ApiOperation("页查询") public ResultPage<String> selectPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { PageInfo pageInfo = businessService.selectPage(pageNum, pageSize); return new ResultPage<Docker>(ResultCodeEnum.SUCCESS.getCode(), "信息查询成功", pageInfo.getPageNum(),pageInfo.getPages(), pageInfo.getTotal(),pageInfo.getPageSize(), pageInfo.getList()); }
用page.getPages()和getTotal()获取页数和记录总数
@Override public PageInfo selectPage(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum, pageSize); List<String> StringList = businessInformationDao.selectAllStoreNumber(); PageInfo<String> pageInfo = new PageInfo<>(StringList); return pageInfo; }
2.mongodb的分页查询
前端传递参数的时候会传递页的参数:
page: {pageSize: 15, currentPage: 1, totalRecord: 0}
currentPage: 1
pageSize: 15
totalRecord: 0
后端代码:主要通过参数,调用skip方法和limit方法实现分页查询
//分页
if (page != null) {
query2.skip(page.getPageNum());
query2.limit(page.getPageSize());
}
//查询实例
List<Map> maps = mongoTemplate.find(query2, Map.class, "dynamic_collection");
3. 页类
public class Page {
private Integer currentPage;
private Integer pageSize;
private Long totalRecord;
public Page(Integer currentPage, Integer pageSize, Long totalRecord) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalRecord = totalRecord;
}
public Page() {
}
public Integer getPageNum() {
return (currentPage - 1) * pageSize;
}
}