使用github的pagehelper分页依赖
<!-- 分页控件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.3.0</version><scope>compile</scope></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.1</version><exclusions><exclusion><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></exclusion><exclusion><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId></exclusion></exclusions></dependency>
分页方式一:使用pageheler直接对查询语句生效(如果查询后还需对数据进行二次处理,此分页方式失效)
public PageInfo<AlarmReportInfo> getReportInfoList(ReportInfoVO info) {//分页操作if (info.getPage() != null && info.getRows() != null) {PageHelper.startPage(info.getPage(), info.getRows());}//查询出系统原有所有的List<AlarmReportInfo> reportInfoAllList = b_alarmReportInfoMapper.getReportInfoList(info);PageInfo<AlarmReportInfo> data = new PageInfo<>(reportInfoAllList );data.setTotal(reportInfoAllList .size());return data;
此时分页只针对b_alarmReportInfoMapper.getReportInfoList(info)函数生效,得到的结果就是分页之后的结果。
分页方式二:先合并数据,再进行分页(此方式适用于对数据进行二次处理的应用)
public PageInfo<AlarmReportInfo> getReportInfoList(ReportInfoVO info) {// 查询系统原有所有的报告List<AlarmReportInfo> reportInfoAllList = b_alarmReportInfoMapper.getReportInfoList(info);// 手动分页int total = reportInfoAllList .size(); // 记录总数int fromIndex = (info.getPage() - 1) * info.getRows();int toIndex = Math.min(fromIndex + info.getRows(), total);// 防止下标越界if (fromIndex > total) {fromIndex = total;toIndex = total;}List<AlarmReportInfo> pageData = reportInfoAllList .subList(fromIndex, toIndex);// 使用 PageInfo 包装分页后的数据PageInfo<AlarmReportInfo> data = new PageInfo<>(pageData);data.setTotal(total); // 设置总记录数return data;
}
或者使用
public PageInfo<AlarmReportInfo> getReportInfoList(ReportInfoVO info) {// 查询系统原有所有的报告List<AlarmReportInfo> reportInfoAllList = b_alarmReportInfoMapper.getReportInfoList(info);// 手动分页int total = reportInfoAllList.size(); // 记录总数int size = reportInfoAllList.size();if (info.getPage() != null && info.getRows() != null) {reportInfoAllList= reportInfoAllList.subList((info.getPage() - 1) * info.getRows(), ((info.getPage() - 1) * info.getRows()) + info.getRows() > reportInfoAllList.size() ? reportInfoAllList.size() : ((info.getPage() - 1) * info.getRows()) + info.getRows());}// 使用 PageInfo 包装分页后的数据PageInfo<AlarmReportInfo> data = new PageInfo<>(reportInfoAllList);data.setTotal(total); // 设置总记录数return data;
}