首先新建一个实体类封装一个方法
/**
*
* @param size2 每页显示的条数
* @param page 页码
* @param list 要进行分页的list集合
* @return
*/
public static List<ParkingCarRecerdReq> pageInfo(int page, int pageSize, List<ParkingCarRecerdReq> list) {
int count = pageSize; // 每页多少条
int size = list.size(); // 总条数
int pageCount = size % count == 0 ? size / count : size / count + 1; // 总页数
int fromIndex = count * (page - 1);
int toIndex = fromIndex + count;
if (toIndex >= size) {
toIndex = size;
}
if (page > pageCount + 1) {
fromIndex = 0;
toIndex = 0;
}
// 分页之后 索引 (包含)(不包含)
List<ParkingCarRecerdReq> listTo= list.subList(fromIndex, toIndex);
return listTo;
}
public JSONObject selectByParkingCarRecerd(ParkingCarRecerdReq r) {
JSONObject json = new JSONObject();
int count = 0;
List lists = new ArrayList();
List<ParkingCarRecerd> list = null;
List<PrakingSite> p = prakingSiteMapper.selectByParkingDealer(r.getDealer());
if (p.size() == 0) {
json.put("resCode", 1);
json.put("result", "暂无数据");
} else {
for (PrakingSite prakingSite : p) {
if (r.getDealer() == null) {
r.setSid(prakingSite.getSid());
r.setStatus("1");
list = parkingCarRecerdMapper.selectByParkingCarRecerdss(r);
for (ParkingCarRecerd parkingCarRecerd : list) {
lists.add(parkingCarRecerd);// 把单个数据一个一个装进list中
count += 1;
}
} else {
r.setSid(prakingSite.getSid());
r.setStatus("1");
list = parkingCarRecerdMapper.selectByParkingCarRecerd(r);
for (ParkingCarRecerd parkingCarRecerd : list) {
lists.add(parkingCarRecerd);// 把单个数据一个一个装进list中
count += 1;
}
}
}
List<ParkingCarRecerdReq> ls = r.pageInfo(r.getPage(), r.getPageSize(), lists);//把分页传进封装的方法里面
json.put("resCode", 0);// 物理内存分页
json.put("count", count);
json.put("result", ls);
}
return json;
}