MybatisPlus分页

在做项目的过程中会涉及到分页的情况,有时用户操作界面时还会手动操作页数和每页的条数,这时就需要下面的操作

Controller层

//分页需要配置mybatisplus的拦截器
    @GetMapping
    public Page<Product> getProductsByPage(
                                            Product product,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 调用业务层的分页查询方法
        Page<Product> productPage = productService.getProByPage(product, pageNum, pageSize);
        if (productPage == null) throw new RuntimeException("输出为空");
        System.out.println("controller"+productPage);
        // 返回分页结果
        return productPage;
    }

Dao层

Page<Product> getAllProducts(Pageable pageable);

Service层

    Page<Product> getProByPage(Product pro, Integer pageNum, Integer pageSize);

impl

@Override
    public Page<Product> getProByPage(Product pro, Integer pageNum, Integer pageSize) {
        Page<Product> productPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productLambdaQueryWrapper
                .eq(!StringUtils.isEmpty(pro.getCoding()), Product::getCoding, pro.getCoding())
                .eq(!StringUtils.isEmpty(pro.getModel()), Product::getModel, pro.getModel())
                .eq(!StringUtils.isEmpty(pro.getClassify()), Product::getClassify, pro.getClassify())
                .eq(!StringUtils.isEmpty(pro.getBrand()), Product::getCoding, pro.getCoding())
                .eq(!StringUtils.isEmpty(pro.getColor()), Product::getColor, pro.getColor());

        // 查询符合条件的产品数据,并进行分页
        Page<Product> pageResult = productDao.selectPage(productPage, productLambdaQueryWrapper);
        System.out.println("ssss");
        // 将查询结果设置到分页对象中
        System.out.println(productPage.setRecords(pageResult.getRecords()));
        System.out.println(productPage.setTotal(pageResult.getTotal()));
        System.out.println(productPage.setPages(pageResult.getPages()));

        return pageResult;
    }

MyBatisPlus分页拦截器

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}
Page<Product>

这里是对数据进行包装,需要传递的参数,就是实体类,第几页,每页显示的条数

这里面也可以包装进行响应的数据,意思就是返回,httpstatus.ok这种

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值