TK-MyBatis分页查询怎么使用
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容介绍了"TK-MyBatis分页查询怎么使用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!记
千家信息网最后更新 2026年11月01日TK-MyBatis分页查询怎么使用
本篇内容介绍了"TK-MyBatis分页查询怎么使用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
记 tkMybatis 查询出一个 List集合 该集合已经做好了一层分页Page封装 即查询出的list 使用类型判断 instanceof Page 为true
但是,中途不明白这是一个带分页的集合,把查询出的结果集又做了一层封装 需要返回的对象类型为GoodsCategoryDTO,代码如下:
// 商品集合 ListgoodsCategorys = 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()); // 设置分页值 // 返回值 ListgoodsCategoryDTO = goodsService.getGoodsCategoryInfo(goodsParam); PageInfo pageInfo = new PageInfo<>(goodsCategoryDTO); return ResultGenerator.genSuccessResult("返回数据成功", pageInfo);
但是万万没有想到,在创建分页对象PageInfo过程中 goodsCategoryDTO这个集合来判断分页形式,然后根据page 获取当前页pageNum和pageSize和pageTotal,但是如果goodsCategoryDTO 使用instanceof 判断一直是Collection 无法分页。
public PageInfo(Listlist, 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 ListgetGoodsCategoryInfo (GoodsParam goodsParam){ // 查询出已经实现和封装好的Page的list List goodsLists = queryGoodsByCat(goodsParam); if (goodsLists instanceof Page>) { Page goodsPage = (Page ) goodsLists; return new Page () {{ 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分页查询怎么使用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
查询
对象
封装
商品
实际
代码
内容
时候
更多
知识
类型
结果
过程
实用
特殊
成功
学有所成
接下来
价格
信息
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
南京欣网互联网络科技
互联网电商科技
互联网和科技创新
互联网科技财经
关于新基建和互联网科技
贷财互联网科技
互联网科技公司招聘
华中科技有哪些互联网大佬
互联网金融科技公司干啥的
互联网金融中的现代科技
港股科技和互联网
恒生互联网科技指数跳水
特约风豹互联网科技为什么扣费
中科明德北京互联网科技
中天科技中国工业互联网
张召忠谈中国互联网科技
图游互联网科技
现代互联网科技公司
互联网的黑科技实验室
互联网科技快速发展优美句子
广东万易互联网科技有限公司惠州分公司
上海睿民互联网科技有限公司
互联网科技卖菜
深圳互联网科技怎么样
须眉科技是互联网公司吗
南山科技园互联网公司有哪些
移动互联网年度科技创新
想开互联网科技公司怎么样
民营互联网公司国防科技
科技大学互联网