实际项目里遇到的问题
如何实现spring下的list查询分页
此文并非教程, 而是个人学习
在common目录下utils 新建一个xxxUtils.class
输入一下的代码(百度来的)
传值传 “page” 到你需要实现的地方 如impl代码里
返回值是一个list集合 主要目的是实现与前端交互时回传的数据有多少
/**
*
* @param page
* @return list集合
*/
public static List<ExcelDataEntity> page(List<ExcelDataEntity> datalist, Integer page, Integer size) {
List<ExcelDataEntity> list = new ArrayList<>();
if (datalist != null && datalist.size() > 0) {
int cuurIdx = (page > 1 ? (page - 1) * size : 0);
for (int i = 0; i < size && i < datalist.size() - cuurIdx; i++) {
ExcelDataEntity user = datalist.get(cuurIdx + i);
list.add(user);
}
}
return list;//2020.11.11 集合分类
}
在你需要实现的impl里加上这样的初始定义
先接收传过来的数据 serviceimpl中用string给其定义
params.get("page");
params.get("limit");
String pages = (String) params.get("page");//从utils传过来page的参数
long page = Long.parseLong(pages);
String limits = (String) params.get("limit");
long limit1 = Long.parseLong(limits);
具体实现方法 在impl中
List<ExcelDataEntity> data = DateUtils.page(dataList, (int) page, (int) limit1);
该impl中 具体代码用到的位置
添加位置 “!”标识出了
@Override
public HashMap<String, Object> alarmFunction(Map<String, Object> params) throws ParseException {
List categorys = null;
List departments = null;
HashMap<String, Object> dataMap = new HashMap<>();
String likeKey = StringUtils.defaultIfEmpty((String) params.get("likeKey"), "name");
!!!String pages = (String) params.get("page");//从utils传过来page的参数
!!!long page = Long.parseLong(pages);
!!!String limits = (String) params.get("limit");
!!!long limit1 = Long.parseLong(limits);
String likeValue = (String) params.get("likeValue");
likeValue = "%" + likeValue + "%";
//分页参数
Long curPage = 0L;
Long limit = 10L;
if (params.get(Constant.PAGE) != null) {
curPage = Long.parseLong((String) params.get(Constant.PAGE)) - 1L;
}
if (params.get(Constant.LIMIT) != null) {
limit = Long.parseLong((String) params.get(Constant.LIMIT));
}
Long start = curPage * limit;
String json = (String) params.get("preciseValue");
if (!StringUtils.isEmpty(json)) {
HashMap<String, String> map = JSON.parseObject(json, HashMap.class);
Set<String> keys = map.keySet();
for (String key : keys) {
switch (key) {
case "category":
String categorysString = map.get(key);
if (StringUtils.isEmpty(categorysString)) {
break;
}
categorys = Arrays.asList(categorysString.split(","));
break;
case "department":
String departmentsString = map.get(key);
if (StringUtils.isEmpty(departmentsString)) {
break;
}
departments = Arrays.asList(departmentsString.split(","));
break;
}
}
}
List<String> unit = UserUtils.getUserDepartment();
List<ApparatusInfoEntity> entitiesInfo = apparatusInfoDao.getAllByKeys(likeKey, likeValue, start, limit, categorys, departments, unit);
List<ExcelDataEntity> dataList = new ArrayList<>();
for (int i = 0; i < entitiesInfo.size(); i++) {
ApparatusInfoEntity entity = entitiesInfo.get(i);
for (int j = 1; j < 8; j++) {
ExcelDataEntity data = new ExcelDataEntity();
String s = DateUtils.getPastDate(j);
Date date = DateUtils.stringToDate(s, "yyyy-MM-dd");
ExcelDataEntity type = apparatusProcessDao.selectProcessByDate(date, entity.getId());
if (type == null) {
ExcelDataEntity app = apparatusProcessDao.getTimeByProcess(entity.getId());
if (app != null) {
data.setId(entity.getId());
data.setName(entity.getName());
data.setCategory(entity.getCategory());
data.setModel(entity.getModel());
data.setPosition(entity.getPosition());
data.setPlannedMaintenance(s);
data.setDepartment(entity.getDepartment());
data.setCommissioningtime(entity.getCommissioningtime());
data.setSn(entity.getSn());
data.setExecutor(app.getExecutor());
data.setResponsible(app.getResponsible());
int time = DateUtils.daysBetween(app.getOperationTime(), new Date());
data.setLastSaved(time + "天");
data.setDescription("运转记录");
data.setType(-1L);
dataList.add(data);
} else {
data.setId(entity.getId());
data.setName(entity.getName());
data.setCategory(entity.getCategory());
data.setModel(entity.getModel());
data.setPosition(entity.getPosition());
data.setPlannedMaintenance(s);
data.setDepartment(entity.getDepartment());
data.setCommissioningtime(entity.getCommissioningtime());
data.setSn(entity.getSn());
data.setDescription("运转记录");
data.setType(-1L);
dataList.add(data);
}
}
}
}
!!!List<ExcelDataEntity> data = DateUtils.page(dataList, (int) page, (int) limit1);
//这里调用与回传的的数值就是在!until!包中给出的
dataMap.put("dataList", data);
dataMap.put("num", dataList.size() - 1);
return dataMap;
}
}