一、导入maven依赖
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
二、配置yml
# PageHelper分页插件
pagehelper:
helperDialect: mysql
supportMethodsArguments: true
params: count=countSql
三、编写工具类
/**
* 分页工具类
*
* @author lihua
*/
public class PageUtils extends PageHelper {
private static Page page;
/**
* 设置请求分页数据
*/
public static void startPage(Integer pageNum, Integer pageSize, String orderBy) {
if (!Objects.isNull(pageNum) && !Objects.isNull(pageSize)) {
page = PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(true);
}
}
public static TableDataInfo getDataTable(List<?> list) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(0);
rspData.setRows(list);
rspData.setTotal(page.getTotal());
return rspData;
}
}
/**
* 表格分页数据对象
*
* @author lihua
*/
public class TableDataInfo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 总记录数
*/
private long total;
/**
* 列表数据
*/
private List<?> rows;
/**
* 消息状态码
*/
private int code;
/**
* 消息内容
*/
private String msg;
/**
* 表格数据对象
*/
public TableDataInfo() {
}
/**
* 分页
*
* @param list 列表数据
* @param total 总记录数
*/
public TableDataInfo(List<?> list, int total) {
this.rows = list;
this.total = total;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
四、使用
分页,那个查询需要分页就调用
PageUtils.startPage();
/**
* 查询设备列表
*
* @param device 设备
* @return 设备
*/
@Override
public TableDataInfo selectDeviceList(Device device, User user) {
//获取用户所在的客户,以及所有的子客户
List<Long> clientIds = getClientIds(user);
//分页,那个查询需要分页就调用
PageUtils.startPage();
List<Device> devices = deviceMapper.selectDeviceByClientId(device, clientIds);
List<Device> devices1 = devices.stream().peek(d -> {
if (d.getPositionLast() == null || d.getPositionLast().getGpstime() == null) {
d.setDeviceStatus(0);
return;
}
//判断是否离线,10分钟没有更新gps定位时间就是离线
if (d.getPositionLast().getGpstime().getTime() + (10 * 60 * 1000) < System.currentTimeMillis()) {
d.setDeviceStatus(2);
} else {
d.setDeviceStatus(1);
}
}).collect(Collectors.toList());
return PageUtils.getDataTable(devices1);
}