两种方式,一种是currentPage + pageSize, 一种是limit + offset
(limit + offset 这个逻辑太恶心,边缘测试很麻烦)
第一种(currentPage + pageSize),共有三个工具类:
package com.operation.utils;
import java.util.List;
public class PagingResult {
private List resultList;
private Integer pageCount;
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
}
package com.operation.utils;
import java.util.List;
public class PagingUtil {
public static List page(Integer pageSize, Integer currentPage, List list){
if(list==null)
return null;
if(list.size()<=pageSize)
return list;
if(pageSize == 0 && currentPage == 0)
return list;
if(list.size()<=(pageSize*(currentPage-1)+pageSize))
return list.subList(pageSize*(currentPage-1),list.size());
return list.subList(pageSize*(currentPage-1),pageSize*(currentPage-1)+pageSize);
}
}
package com.operation.utils;
import java.util.List;
public class PagingUtil2 {
public static PagingResult page(Integer pageSize, Integer currentPage, List list){
PagingResult pagingResult=new PagingResult();
if(list==null){
return null;
}
if(list.size()<=pageSize){
pagingResult.setResultList(list);
pagingResult.setPageCount(1);
return pagingResult;
}
if(pageSize == 0 && currentPage == 0){
pagingResult.setResultList(list);
pagingResult.setPageCount(1);
return pagingResult;
}
if(list.size()<=(pageSize*(currentPage-1)+pageSize)){
List sbuList=list.subList(pageSize*(currentPage-1),list.size());
pagingResult.setResultList(sbuList);
pagingResult.setPageCount(list.size()/pageSize+1);
return pagingResult;
}
List sbuList=list.subList(pageSize*(currentPage-1),pageSize*(currentPage-1)+pageSize);
pagingResult.setResultList(sbuList);
pagingResult.setPageCount(list.size()/pageSize+1);
return pagingResult;
}
}
添加这三个工具类之后,在controller中的使用方式如下:
@RequestMapping(value = "/getTestList",method = RequestMethod.GET)
public void getTestList(ModelMap modelMap, Integer currentPage, Integer pageSize, HttpServletResponse response) throws IOException {
List<Test> testList=testService.getTestList();
boolean res = !testList.isEmpty();
String listName = "testList";
modelMap.put(listName,testList);
JSONObject jsonObject = JSONUtil.returnArrJson(res, currentPage, pageSize, testList, listName);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(jsonObject);
}
第二种(limit + offset)使用方式:
@RequestMapping(value = "/getTestListByName",method = RequestMethod.GET)
public void getTestListByName(ModelMap modelMap, String foodName, Integer limit, Integer offset, HttpServletResponse response, HttpServletRequest request) throws IOException {
JSONObject rawJson=testSearchService.strongSearch(testName);
List<JSONObject> testList = testSearchService.getTestList(rawJson);
String listName = "data";
String paramName = "testName="+testName;
modelMap.put(listName, testList);
JSONObject jsonObject = JSONUtil.returnArrJson2(limit, offset, foodList, listName, request.getRequestURL().append("?").append(paramName));
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(jsonObject);
}
再友情送一个JSONUtil类:
package com.operation.utils;
import org.apache.commons.collections4.CollectionUtils;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class JSONUtil {
public static JSONObject returnBoolJson(boolean jsonres){
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",200);
jsonObject.put("msg","success");
jsonObject.put("success",jsonres);
jsonObject.put("result","operation " + jsonres);
return jsonObject;
}
public static JSONObject returnStringJson(boolean res, String jsonres){
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",200);
jsonObject.put("msg","success");
jsonObject.put("success",res);
jsonObject.put("result",jsonres);
return jsonObject;
}
public static JSONObject returnArrJson(boolean res, Integer currentPage, Integer pageSize, List resultList, String listName){
JSONObject jsonObject = new JSONObject();
PagingResult pagingResult;
if (res) {
if (pageSize == null || currentPage == null)
pagingResult = PagingUtil2.page(Integer.MAX_VALUE, 1, resultList);
else
pagingResult = PagingUtil2.page(pageSize, currentPage, resultList);
if (CollectionUtils.isNotEmpty(resultList)) {
jsonObject.put("code", 200);
jsonObject.put("msg", "查询成功!");
assert pagingResult != null;
jsonObject.put("currentPage", currentPage);
jsonObject.put("pageCount", pagingResult.getPageCount());
jsonObject.put("result", new JSONObject().put(listName, pagingResult.getResultList()));
} else {
jsonObject.put("code", 404);
jsonObject.put("msg", "查询失败!");
jsonObject.put("result", new JSONObject());
}
}else {
jsonObject.put("code",404);
jsonObject.put("msg","查询失败!");
jsonObject.put("result",new JSONObject());
}
return jsonObject;
}
public static JSONObject returnObjJson(boolean res, JSONObject json){
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",200);
jsonObject.put("msg","success");
jsonObject.put("success",res);
jsonObject.put("result",json);
return jsonObject;
}
public static JSONObject returnArrJson2(Integer limit, Integer offset, List<JSONObject> objectList, String listName, StringBuffer baseUrl) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("count", objectList.size());
StringBuffer next = new StringBuffer();
StringBuffer previous = new StringBuffer();
List<JSONObject> resultlist = new ArrayList<>();
Integer MAX_VALUE = 500;
if (offset == null && limit == null || offset == null && limit >= MAX_VALUE || offset == null || offset == 0 && limit >= MAX_VALUE){
if (objectList.size() <= 15)
next.append("null");
else
next.append(baseUrl).append("&limit=15&offset=15");
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList;
previous.append("null");
}else {
if (offset >= MAX_VALUE && limit == null){
if (objectList.size() <= 15)
next.append("null");
else
next.append(baseUrl).append("&limit=15&offset=15");
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList.subList(offset >= 15 ? offset - 15 : 0, offset);
previous.append("null");
} else if (limit >= MAX_VALUE && offset >= MAX_VALUE) {
next.append("null");
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList.subList(offset >= 15 ? offset - 15 : 0, offset);
previous.append(baseUrl).append("&limit=999999");
} else if (limit == 0 && offset == 0) {
if (objectList.size() <= 15)
next.append("null");
else
next.append(baseUrl).append("&limit=15&offset=15");
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList;
previous.append("null");
} else if (offset >= MAX_VALUE && limit == 0) {
next.append("null");
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList.subList(offset >= 15 ? offset - 15 : 0, offset);
previous.append(baseUrl).append("&limit=0&offset=999999");
} else if (limit == 0) {
if (objectList.size() <= 15)
next.append("null");
else
next.append(baseUrl).append("&limit=0&offset=").append(offset + 15);
resultlist = objectList.size() >= 16 ? objectList.subList(0, 15) : objectList.subList(offset >= 15 ? offset - 15 : 0, offset);;
previous.append(baseUrl).append("&limit=0&offset=").append(offset >= 15 ? offset - 15 : 0);
} else if (limit >= objectList.size()){
next.append("null");
resultlist = objectList;
previous.append("null");
} else {
next.append(baseUrl).append("&limit=").append(limit).append("&offset=").append(offset - limit);
resultlist = objectList.subList(offset >= objectList.size() ? 0 : offset, limit + offset >= objectList.size() ? objectList.size() : limit + offset);
previous.append(baseUrl).append("&limit=").append(limit).append("&offset=").append(limit + offset);
}
}
jsonObject.put("next", next);
jsonObject.put(listName, resultlist);
jsonObject.put("previous", previous);
return jsonObject;
}
}
Ps. 使用方式是在Spring+SpringMVC的环境中使用的,在controller代码中,改一改也可以用在其他框架或者原生Servlet里