使用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;
}