博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis-plus之RowBounds实现分页查询
阅读量:7079 次
发布时间:2019-06-28

本文共 3236 字,大约阅读时间需要 10 分钟。

物理分页和逻辑分页

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

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

优缺点

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

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

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

一般用法

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

Mapper层

1 List
selectList(RowBounds rowBounds);
1 

转载地址:http://fycml.baihongyu.com/

你可能感兴趣的文章
REGEXP_EXTRACT
查看>>
购物网站常见的展示商品的div
查看>>
iOS开发~CocoaPods使用详细说明
查看>>
Confluence 6 使用 CSS 样式化 Confluence 的介绍
查看>>
Go 语言开源发布 9 周年!社区贡献指数创新高
查看>>
MySQL中的账号与权限管理
查看>>
Java原生实现定时器
查看>>
【我们一起写框架】MVVM的WPF框架(三)—数据控件
查看>>
Confluence 6 导入模板的定义
查看>>
java中迭代器的原理图解和源码浅析
查看>>
EditPlus如何设置保存时不产生.bak备份文件?
查看>>
机器学习到底是什么?
查看>>
phpstorm配置svn
查看>>
如何使用DGBroker关闭redo应用(1)
查看>>
原来,在Linux系统也有快速格式化功能
查看>>
Hashtable:仅有两列的表
查看>>
用ISAPI Filter设置HttpOnly属性
查看>>
DNS域名服务器
查看>>
springmvc4环境简单搭建和定时任务
查看>>
听说你刚中了NIPS?恭喜(研究德扑、老鼠胡须等AI的都入围了)
查看>>