当前位置: 首页 > news >正文

Mybatis-plus之RowBounds实现分页查询

物理分页和逻辑分页

物理分页:直接从数据库中拿出我们需要的数据,例如在Mysql中使用limit。

逻辑分页:从数据库中拿出所有符合要求的数据,然后再从这些数据中拿到我们需要的分页数据。

优缺点

物理分页每次都要访问数据库,逻辑分页只访问一次。

物理分页占用内存少,逻辑分页相对较多。

物理分页数据每次都是最新的,逻辑分页有可能滞后。

一般用法

1 public List<Order> queryListByPage(RowBounds rowBounds);
1 dao.queryListPage(new RowBounds(offset,limit));

RowBounds对象有2个属性,offset和limit。

offset:起始行数

limit:需要的数据行数

因此,取出来的数据就是:从第offset+1行开始,取limit行

Mybatis中使用RowBounds实现分页的大体思路:

先取出所有数据,然后游标移动到offset位置,循环取limit条数据,然后把剩下的数据舍弃。

 1 private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
 2     DefaultResultContext<Object> resultContext = new DefaultResultContext();
 3     this.skipRows(rsw.getResultSet(), rowBounds);  //游标跳到offset位置
 4     //取出limit条数据
 5     while(this.shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
 6         ResultMap discriminatedResultMap = this.resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, (String)null);
 7         Object rowValue = this.getRowValue(rsw, discriminatedResultMap);
 8         this.storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
 9     }
10 
11 }
 1 private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
 2     if (rs.getType() != 1003) {
 3         if (rowBounds.getOffset() != 0) {
 4             rs.absolute(rowBounds.getOffset());
 5         }
 6     } else {     //从头开始移动游标,直至offset位置
 7         for(int i = 0; i < rowBounds.getOffset(); ++i) {
 8             rs.next();
 9         }
10     }
11 
12 }

在Mybatis-Plus中的应用

Controller层

 1 @RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
 2 @PageableDefaults(sort = "createDate=desc")
 3 private void getList(Queryable queryable,String queryStr, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
 4                       HttpServletResponse response) throws IOException {
 5     //前端传过来需要的参数,加上id,fastjson会在得到结果集时过滤数据
 6     propertyPreFilterable.addQueryProperty("id");
 7     QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
 8     SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
 9     //调用service层的分页查询
10     PageJson<OprPrintOrder> pagejson = new PageJson<OprPrintOrder>(service.list(queryable));
11     //得到需要的结果集后的数据过滤操作
12     String content = JSON.toJSONString(pagejson, filter);
13     JSONObject result = JSONObject.parseObject(content);
14     StringUtils.printJson(response, result.toString());
15 }

Service层

 1 @Override
 2 public Page<Order> list(Queryable queryable) {
 3     //pageable中有数据查询的要求
 4     Pageable pageable = queryable.getPageable();
 5     //封装新的分页查询类
 6     com.baomidou.mybatisplus.plugins.Page<Order> page = new com.baomidou.mybatisplus.plugins.Page<Order>(pageable.getPageNumber(), pageable.getPageSize());
 7     //传入RowBounds,page就是RowBounds的子类,这样查询后page就有了总页数与总条数
 8     page.setRecords(mapper.selectList(page));
 9     return new PageImpl<Order>(page.getRecords(), pageable, page.getTotal());
10 }

Mapper层

1 List<Order> selectList(RowBounds rowBounds);
1 <select id="selectList" resultType="Order">
2   select * from order
3 </select>

相关文章:

  • clover无缘无故隐藏书签栏原因
  • Vue 路由切换时页面内容没有重新加载
  • 303. Range Sum Query - Immutable
  • 从“被动挖光缆”到“主动剪网线”,蚂蚁金服异地多活的微服务体系
  • 冲刺第四天 11.28 WED
  • Spark 用户自定义函数 Java 示例
  • jQuery焦点图插件
  • Go之路
  • 全栈开发——Linux
  • cfile fopen fopen_s win10下打开文件失败
  • Android实现摇晃手机的监听(摇一摇)
  • SVN服务器迁移实战
  • Ceisum官方教程2 -- 项目实例(workshop)
  • 月薪1.5W以下的数据分析师·面试流程(附赠考题)
  • 事故现场:mysql自增id超大问题查询
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • angular2 简述
  • Git的一些常用操作
  • iOS 系统授权开发
  • js ES6 求数组的交集,并集,还有差集
  • Js基础——数据类型之Null和Undefined
  • Meteor的表单提交:Form
  • Service Worker
  • SpiderData 2019年2月23日 DApp数据排行榜
  • vue总结
  • 将 Measurements 和 Units 应用到物理学
  • 巧用 TypeScript (一)
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 如何实现 font-size 的响应式
  • 如何抓住下一波零售风口?看RPA玩转零售自动化
  • 数组大概知多少
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 大数据全解:定义、价值及挑战
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • ​用户画像从0到100的构建思路
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (十六)Flask之蓝图
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)c++ std::pair 与 std::make
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net中应用SQL缓存(实例使用)
  • [ vulhub漏洞复现篇 ] Django SQL注入漏洞复现 CVE-2021-35042
  • [ vulhub漏洞复现篇 ] JBOSS AS 5.x/6.x反序列化远程代码执行漏洞CVE-2017-12149
  • [AR]Vumark(下一代条形码)
  • [BPU部署教程] 教你搞定YOLOV5部署 (版本: 6.2)