Mybatis-plus之RowBounds实现分页查询

Mybatis-plus之RowBounds实现分页查询,第1张

Mybatis-plus之RowBounds实现分页查询 物理分页和逻辑分页

物理分页:直接从数据库中拿出我们需要的数据,例如在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>

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/585642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存