分页插件PageHelper踩坑

2020-08-11  本文已影响0人  wang_cheng

日常踩坑

测试说这个分页总条数total不对,总是等于传入的pageSize大小,一脸懵逼,看下库里确实不止这些数量

开始看代码....
一直不相信这种简单的操作代码有问题....

说明:就是一个列表查询,返回的时候需要拿到数据进行一些转换封装另外一个对象返回出去

看上去一切很正常的操作,能看出问题吗?

       //1.开启分页
         PageHelper.startPage(purchase.getPageNum(), purchase.getPageSize());

        //2.查数据
        List<TGmShoesCardExchangePurchaseRecord> list = tGmShoesCardExchangePurchaseRecordMapper.selectByExample(example);

        PageInfo<TGmShoesCardExchangePurchaseRecord> pageInfo = new PageInfo<>(list);
        //3.查到数据进行转换封装返回
        List<CardExchangePurchaseDTO> collect = list.stream().map(record -> {
            CardExchangePurchaseDTO info = new CardExchangePurchaseDTO();
            BeanUtils.copyProperties(record, info);
            JSONObject jsonObject = JSON.parseObject((String) record.getSnapShot());
            info.setExchangeTime(record.getCreateTime().getTime());
            info.setSkuValue(jsonObject.getString("skuValue"));
            info.setReceiverName(jsonObject.getString("receiverName"));
            info.setReceiverPhone(jsonObject.getString("receiverPhone"));
            info.setAddress(jsonObject.getString("address"));
            return info;
        }).collect(Collectors.toList());

        //4.返回数据
        return new PageInfo<>(collect);


看出来了吗?

开启分页的时候是对数据层对象进行查询然后会返回一个Page对象,这个page设置了一些分页的属性
Page<Object> page = PageHelper.startPage(purchase.getPageNum(), purchase.getPageSize());

查询数据库
List<TGmShoesCardExchangePurchaseRecord> list = tGmShoesCardExchangePurchaseRecordMapper.selectByExample(example)
如果不转换的话,应该是将这个查到的对象直接放到PageInfo构造器中返回
return new PageInfo<>(list)

看下new PageInfo()构造函数源码


 public PageInfo(List<T> list) {
        this(list, 8);
    }

//看下this方法
public PageInfo(List<T> list, int navigatePages) {
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;

      //重点,如果list是属于Page对象,那分页信息从这个Page对象里获取,没问题的
        if (list instanceof Page) {
            Page page = (Page)list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
            this.total = page.getTotal();
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                this.endRow = this.startRow - 1 + this.size;
            }
      //这里,如果是新建的一个List(因为我进行数据转换,这个List是新建的属于Collection),所以分页属性
    //直接以list.size()去进行判断,看this.total=list.size(); 并不是数据库的实际总条数
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.list = list;
            this.size = list.size();
            this.total = (long)list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            this.calcNavigatepageNums();
            this.calcPage();
            this.judgePageBoudary();
        }

    }

那一般我们需要拿到数据还需要进行处理,这里只针对不对数据进行过滤筛选,只转换,可以进行重新设值

  PageHelper.startPage(purchase.getPageNum(), purchase.getPageSize());

        List<TGmShoesCardExchangePurchaseRecord> list = tGmShoesCardExchangePurchaseRecordMapper.selectByExample(example);

    
        //这边先组装PageInfo
        PageInfo<TGmShoesCardExchangePurchaseRecord> pageInfo = new PageInfo<>(list);

        List<CardExchangePurchaseDTO> collect = list.stream().map(record -> {
            CardExchangePurchaseDTO info = new CardExchangePurchaseDTO();
            BeanUtils.copyProperties(record, info);
            JSONObject jsonObject = JSON.parseObject((String) record.getSnapShot());
            info.setExchangeTime(record.getCreateTime().getTime());
            info.setSkuValue(jsonObject.getString("skuValue"));
            info.setReceiverName(jsonObject.getString("receiverName"));
            info.setReceiverPhone(jsonObject.getString("receiverPhone"));
            info.setAddress(jsonObject.getString("address"));
            return info;
        }).collect(Collectors.toList());

        //踩坑,分页后数据要组装转换,分页信息需要重新设置,不然会导致总页数=list.size()
        //将需要转换封装的类new一个出来,下面进行属性copy重新设值
        PageInfo<CardExchangePurchaseDTO> page = new PageInfo<>(collect);
        BeanUtils.copyProperties(pageInfo,page);
        page.setList(collect);
        return page;

上一篇下一篇

猜你喜欢

热点阅读