在解决PageHelper.startPage(page, size);的位置问题后,又遇到total等信息错误,主要还是不细心,两个不同的List实例化进PageInfo,不能混在一起,同时要确保这两个List的PageInfo相同(例如:total等page信息相同),才能 pageInfo.setList(stockGoodsDtos);
问题代码:
Controller.java
public Wrapper<PageInfo<StockGoodsDto>> 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<StockGoodsDto> list = shopGoodsService.getStockGoodsByCondition(shopGoods, page, size);
PageInfo pageInfo = new PageInfo(list);
return WrapMapper.ok(pageInfo);
}
ServiceImpl.java
public List<StockGoodsDto> getStockGoodsByCondition(ShopGoods shopGoods, Integer page , Integer size) {
List<StockGoodsDto> stockGoodsDtos = new ArrayList<>();
...
PageHelper.startPage(page, size);
List<ShopGoods> 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<PageInfo<StockGoodsDto>> 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<StockGoodsDto> getStockGoodsByCondition(ShopGoods shopGoods, Integer page , Integer size) {
List<StockGoodsDto> stockGoodsDtos = new ArrayList<>();
...
PageHelper.startPage(page, size);
List<ShopGoods> 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;
}