温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

TK-MyBatis分页查询怎么使用

发布时间:2021-12-01 13:34:39 来源:亿速云 阅读:576 作者:iii 栏目:开发技术

本篇内容介绍了“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");
         }
    }

“TK-MyBatis分页查询怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI