在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页。今天重点给大家说一下后台分页。
Mybatis给我们提供了分页插件PageHelper,下面是使用PageHelper具体步骤:

步骤一:配置pom依赖

<dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper</artifactId>
     <version>4.1.4</version>
 </dependency>

步骤二:分页

@ApiOperation(value =\"根据查询条件获取燃气爆炸数据列表\")
@PostMapping(\"/getAccidentList\")
public   getAccidentList(@RequestBody Map<String, String> cdt){
    Example example = new Example(Plenv燃气爆炸.class);
    Example.Criteria criteria = example.createCriteria();
    if(cdt!=null){
        if(!StringUtil.isEmpty(cdt.get(\"oid\"))){
            criteria.andEqualTo(\"oid\",cdt.get(\"oid\"));
        }
        if(!StringUtil.isEmpty(cdt.get(\" \"))){
            criteria.andLike(\"标题\",\"%\"+cdt.get(\" \")+\"%\");
        }
    }
    example.orderBy(\"时间\").desc();
    //传分页参数
    PageHelper.startPage(Integer.parseInt(cdt.get(\"pageStart\")), Integer.parseInt(cdt.get(\"pageSize\")));

    List<Plenv燃气爆炸> accidentList = plenv燃气爆炸Mapper.selectByExample(example);
    // 取分页信息
    PageInfo<Plenv燃气爆炸> pageInfo = new PageInfo<Plenv燃气爆炸>(accidentList);
    return JSONResult.ok(accidentList);
}

PageHelper的优点是,分页和Mapper. 完全解耦。实现方式是以插件的形式,对Mybatis执行的流程进行了强化,添加了总数count和limit查询。属于物理分页。
PageHelper.startPage(pageStart, pageSize) pageStart- 第N页, pageSize - 每页M条数

收藏 打印