TK-MyBatis 分页查询

记 tkMybatis 查询出一个  List集合 该集合已经做好了一层分页Page封装 即查询出的list 使用类型判断 instanceof Page 为true
但是,中途不明白这是一个带分页的集合,把查询出的结果集又做了一层封装 需要返回的对象类型为GoodsCategoryDTO,代码如下: 

   // 商品集合
             List<GoodsCategoryDTO> goodsCategorys  = goodsLists.stream().map(x->{
            GoodsCategoryDTO goodsDTO = new GoodsCategoryDTO();
            goodsDTO.setGoodsId(x.getGoodsId());
            // 价格,名字
            if(x.getPriceInfo().getPriceValue() == null && x.getPriceInfo().getPriceType() == null){
                goodsDTO.setOriginalPrice(x.getPriceInfo().getOriginalPrice());
            }else {
                goodsDTO.setOriginalPrice(x.getPriceInfo().getOriginalPrice());
                goodsDTO.setPrice(x.getPriceInfo().getPriceValue() == null ?  new BigDecimal(""): x.getPriceInfo().getPriceValue());
                goodsDTO.setGoodsLable(x.getPriceInfo().getPriceName());
            }
            goodsDTO.setTitle(x.getGoodsName());
            goodsDTO.setSubTitle(x.getGoodsSubname());
            goodsDTO.setImg(x.getGoodsImage());
            goodsDTO.setSkuId(x.getSkuId());
            return goodsDTO;
        }).collect(Collectors.toList());
         return goodsCategorys;


从字面意思上理解 没有问题 返回一个需要出里商品的集合 在controller里面做分页的时候new 一个PageInfo 把该集合传入 代码如下:
       

 PageHelper.startPage(goodsParam.getPage(), goodsParam.getSize());  //  设置分页值
        // 返回值
         List<GoodsCategoryDTO> goodsCategoryDTO = goodsService.getGoodsCategoryInfo(goodsParam);
         PageInfo<GoodsCategoryDTO> pageInfo = new PageInfo<>(goodsCategoryDTO);
         return ResultGenerator.genSuccessResult("返回数据成功", pageInfo);


但是万万没有想到,在创建分页对象PageInfo过程中 goodsCategoryDTO这个集合来判断分页形式,然后根据page 获取当前页pageNum和pageSize和pageTotal,但是如果goodsCategoryDTO 使用instanceof 判断一直是Collection 无法分页。
   

public PageInfo(List<T> list, int navigatePages) {
        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();
            //由于结果是>startRow的,所以实际的需要+1
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                //计算实际的endRow(最后一页的时候特殊)
                this.endRow = this.startRow - 1 + this.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 = list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }
        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            //计算导航页
            calcNavigatepageNums();
            //计算前后页,第一页,最后一页
            calcPage();
            //判断页面边界
            judgePageBoudary();
        }
    }


想了一下 使用如下的解决办法可能会好一点 直接new 一个新的Page对象 获取新的pageNum pageSize Total 然后赋值给该对象 ,然后遍历集合中goodsList 然后做二次封装 添加到 page集合 最后返回该集合即可。
 

 /**
     * 查询分类商品信息
     * @param goodsParam
     * @return
     */
    public List<GoodsCategoryDTO> getGoodsCategoryInfo (GoodsParam goodsParam){

         // 查询出已经实现和封装好的Page的list
         List<GoodsDTO> goodsLists = queryGoodsByCat(goodsParam);

         if (goodsLists instanceof Page<?>) {
             Page<GoodsDTO> goodsPage = (Page<GoodsDTO>) goodsLists;
             return new Page<GoodsCategoryDTO>() {{
                 this.setPageNum(goodsPage.getPageNum());
                 this.setPageSize(goodsPage.getPageSize());
                 this.setTotal(goodsPage.getTotal());
                 this.setPages(goodsPage.getPages());
                 this.addAll(goodsPage.stream().map(goods -> new GoodsCategoryDTO() {{
                     this.setGoodsId(goods.getGoodsId());
                     this.setTitle(goods.getGoodsName());
                     this.setSubTitle(goods.getGoodsSubname());
                     this.setImg(goods.getGoodsImage());
                     this.setSkuId(goods.getSkuId());
                     if(goods.getPriceInfo().getPriceValue() == null && goods.getPriceInfo().getPriceType() == null){
                         this.setOriginalPrice(goods.getPriceInfo().getOriginalPrice());
                     }else {
                         this.setOriginalPrice(goods.getPriceInfo().getOriginalPrice());
                         this.setPrice(goods.getPriceInfo().getPriceValue() == null ?  new BigDecimal(""): goods.getPriceInfo().getPriceValue());
                         this.setGoodsLable(goods.getPriceInfo().getPriceName());
                     }
                 }}).collect(Collectors.toList()));
             }};
         } else {
             throw new IllegalStateException("goods list must be instance of Page");
         }
    }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值