物理分页和逻辑分页

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

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

优缺点

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

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

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

一般用法

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

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

offset:起始行数

limit:需要的数据行数

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

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

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

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

在Mybatis-Plus中的应用

Controller层

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

Service层

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

Mapper层

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

Mybatis-plus之RowBounds实现分页查询的更多相关文章

  1. Oracle使用MyBatis中RowBounds实现分页查询

    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便. 使用MyBatis中的RowBounds进行 ...

  2. springboot结合mybatis使用pageHelper插件进行分页查询

    1.pom相关依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  3. Mybatis的ResultMap与limit分页查询

    ResultMap主要解决的是:属性名和字段不一致 如果在pojo中设置的是一个名字,在数据库上又是另一个名字,那么查询出来的结果或者其他操作的结果就为null. //在pojo中 private S ...

  4. MyBatis学习总结_12_Mybatis+Mysql分页查询

    package cn.tsjinrong.fastfile.util; /** * @ClassName: Page * @Description: TODO(分页组件的父类,用来封装分页的 通用内容 ...

  5. Mybatis + SpringMVC + Maven实现分页查询

    使用Mybatis + Maven + SpringMVC 运行时,突然被需要分页查询的功能给难住了 这里推荐采用的插件是PageHelper这个插件,使用起来十分方便.该插件支持以下数据库: Ora ...

  6. Mybatis+SpringMVC实现分页查询(附源码)

    Maven+Mybatis+Spring+SpringMVC实现分页查询(附源码) 一.项目搭建 关于项目搭建,小宝鸽以前写过一篇Spirng+SpringMVC+Maven+Mybatis+MySQ ...

  7. Mybatis包分页查询java公共类

    Mybatis包分页查询java公共类   分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...

  8. JAVA入门[10]-mybatis分页查询

    1.添加分页插件 在mybatis-generator-config.xml添加plugin节点: <plugin type="org.mybatis.generator.plugin ...

  9. Mybatis+MySQL动态分页查询

    https://blog.csdn.net/qq_34137397/article/details/63289621 mybatis有两种分页方法 1.内存分页,也就是假分页.本质是查出所有的数据然后 ...

随机推荐

  1. MQTT协议笔记之mqtt.io项目TCP协议支持

    前言 MQTT定义了物联网传输协议,其标准倾向于原始TCP实现.构建于TCP的上层协议堆栈,诸如HTTP等,在空间上多了一些处理路径,稍微耗费了CPU和内存,虽看似微乎其微,但对很多处理能力不足的嵌入 ...

  2. [css]演示:纯CSS实现的右侧底部简洁悬浮效果

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...

  3. 不同linux下两网卡绑定方法

    记得原来在做性能测试时,为了提高网络吞吐率.必须将两个网卡绑定一起工作.绑定方法如下: 一.CentOS 配置   1.编辑虚拟网络接口配置文件,指定网卡IP: # vi /etc/sysconfig ...

  4. Android动态添加布局

    //1.利用LayoutInflater的inflate动态加载XML mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID) ...

  5. iPad - 开发(Universal Applications)

    一.iPad 1.判断是否在iPad上 BOOL iPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdi ...

  6. 在线预览文档(支持word、excel、ppt、pdf)+在线预览文档html版(转)

    1.首先上网搜索一下有什么解决方案 (1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf (1) a. ...

  7. Yii 的session 实现返回上上页面

    学习session的页面:http://www.yiichina.com/doc/guide/2.0/runtime-sessions-cookies 关键摘要: $session = Yii::$a ...

  8. 第二次去苹果店维修MacBook

    今天上午,在使用外接鼠标的情况下,屏幕上鼠标指针乱窜.乱点.不受控制的故障再次出现,这次拍下了视频. 再次去苹果网站预约Genius Bar(天才吧),中午的时候去了苹果店.这次没有像上次那样检查身份 ...

  9. Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms

    网易公开课,第5课 notes,http://cs229.stanford.edu/notes/cs229-notes2.pdf 学习算法有两种,一种是前面一直看到的,直接对p(y|x; θ)进行建模 ...

  10. Python开发【数据结构】:基础

    数据结构 什么是数据结构? 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表.集合与字典等都是一种数据结构 N.Wirth: “程序=数据结构+算法” 列表 列表:在其他编程 ...