第一种分页(用sql分页)
public static int[] getStartEndPage(int page, int limit) {
int startPage = (page - 1) * limit + 1;
int endPage = page * limit;
int[] startEndPage = {startPage, endPage};
return startEndPage;
}
//计算起始页 int[] startEndPage = PageUtils.getStartEndPage(page, limit); startPage = startEndPage[0]; endPage = startEndPage[1];
Select * from (select ROWNUM rn, t.* from (select * from table) t) where rn between startPage and endPage
第二种分页 (先查询所有,在截取)
public TableResultResponse selectDataManaList(Integer page, Integer limit, String queryStr) { List list = dataManaBiz.selectDataManaList(queryStr);
Integer total = list.size();
Integer maxCount = limit * page;
Integer minCount = limit * (page - 1);
if (maxCount > list.size()) {
maxCount = list.size();
}
list = list.subList(minCount, maxCount);
TableResultResponse tableResultResponse = new TableResultResponse<>(total, list);
tableResultResponse.setMessage("success");
return tableResultResponse;
}
第三种分页(用pageheper插件)
pom com.github.pagehelperpagehelper4.1.4
SqlMapConfig.xml
application.yml mybatis: mapper-locations: classpath:mapper/**/*.xml type-aliases-package: com.clamc.entity config-location: classpath:builder/SqlMapConfig.xml //扫描MyBatis全局变量配置
public PageInfo selectDocByPage1(int currentPage, int pageSize) { PageHelper.startPage(currentPage, pageSize); List docs = docMapper.selectByPageAndSelections(); PageInfo pageInfo = new PageInfo<>(docs); return pageInfo; }