当前位置:主页 > 软件编程 > JAVA代码 >

java仿百度假分页代码实现的详解

时间:2020-11-26 22:53:19 | 栏目:JAVA代码 | 点击:

1 ,service 层代码实现

 Integer start = (pageNum - 1) * pageSize;
    Integer end = pageSize;
    List<entity> entityList;
    
    //调用mapper查询数据库
    entityList = entityPOMapper.query(start, end);

    SimplePageInfo simplePageInfo = SimplePageInfo.of(entityList );
    simplePageInfo.setNumber(pageNum);
    simplePageInfo.setPageSize(pageSize);

// 查询出来数据等于size,总数据量加一,页数加一
    if (entityList .size() == pageSize) {
      Integer totalElements = (pageNum * pageSize) + 1;
      Integer pages = pageNum + 1;
      simplePageInfo.setTotalElements(totalElements);
      simplePageInfo.setPages(pages);
    } else {
    //数据不够size
      Integer totalElements = ((pageNum - 1) * pageSize + entityList .size());
      Integer pages = pageNum;
      simplePageInfo.setTotalElements(totalElements);
      simplePageInfo.setPages(pages);
    }
    return simplePageInfo;
}

2,mapper

<select .............>
    select..
    from ...
    where ...
    LIMIT #{start}, #{end}
  </select>

您可能感兴趣的文章:

相关文章