解决PageHelper.startPage(page, size)后,关于PageInfo的total等属性不正确等问题

在解决PageHelper.startPage(page, size);的位置问题后,又遇到total等信息错误,主要还是不细心,两个不同的List实例化进PageInfo,不能混在一起,同时要确保这两个List的PageInfo相同(例如:total等page信息相同),才能 pageInfo.setList(stockGoodsDtos);

问题代码:

Controller.java

public Wrapper> getStockGoodsByCondition(@RequestBody ShopGoods shopGoods,
                                                                 @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        List list = shopGoodsService.getStockGoodsByCondition(shopGoods, page, size);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

ServiceImpl.java

public List getStockGoodsByCondition(ShopGoods shopGoods, Integer page , Integer size) {
        List stockGoodsDtos = new ArrayList<>();
        ...
        PageHelper.startPage(page, size);
        List shopGoodsList = shopGoodsMapper.getStockGoodsByCondition(shopGoods, specIds);
        if (shopGoodsList == null || shopGoodsList.size() == 0) {
            PageInfo pageInfo = new PageInfo(stockGoodsDtos);
            return pageInfo;
        }
        for (ShopGoods goods : shopGoodsList) {
            StockGoodsDto stockGoodsDto = new StockGoodsDto();
            stockGoodsDto.setShopGoods(goods);
            for (ProdSpecInfoDto specInfoDto : prodSpecInfoDtos) {
                if (goods.getProductId().equals(specInfoDto.getSpecId())) {
                    stockGoodsDto.setProdSpecInfoDto(specInfoDto);
                    stockGoodsDtos.add(stockGoodsDto);
                }
            }
        }
        return stockGoodsDtos;
    }

修改后,正确代码:

Controller.java

public Wrapper> getStockGoodsByCondition(@RequestBody ShopGoods shopGoods,
                                                                 @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        return WrapMapper.ok(shopGoodsService.getStockGoodsByCondition(shopGoods, page, size));
    }

ServiceImpl.java

public PageInfo getStockGoodsByCondition(ShopGoods shopGoods, Integer page , Integer size) {
        List stockGoodsDtos = new ArrayList<>();
        ...
        PageHelper.startPage(page, size);
        List shopGoodsList = shopGoodsMapper.getStockGoodsByCondition(shopGoods, specIds);
        if (shopGoodsList == null || shopGoodsList.size() == 0) {
            PageInfo pageInfo = new PageInfo(stockGoodsDtos);
            return pageInfo;
        }
        for (ShopGoods goods : shopGoodsList) {
            StockGoodsDto stockGoodsDto = new StockGoodsDto();
            stockGoodsDto.setShopGoods(goods);
            for (ProdSpecInfoDto specInfoDto : prodSpecInfoDtos) {
                if (goods.getProductId().equals(specInfoDto.getSpecId())) {
                    stockGoodsDto.setProdSpecInfoDto(specInfoDto);
                    stockGoodsDtos.add(stockGoodsDto);
                }
            }
        }
        PageInfo pageInfo = new PageInfo(shopGoodsList);
        pageInfo.setList(stockGoodsDtos);
        return pageInfo;
    }

转载于:https://www.cnblogs.com/zys-blog/p/10209977.html

你可能感兴趣的:(解决PageHelper.startPage(page, size)后,关于PageInfo的total等属性不正确等问题)