技术篇-分页查询

1. boot的分页查询

使用PageHelper的starter
  1. 在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>
    
  2. 在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
    
  3. 使用 :最后就是使用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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值