Java 分页,两种方式的分页,即取即用的代码,不客气
生活随笔
收集整理的這篇文章主要介紹了
Java 分页,两种方式的分页,即取即用的代码,不客气
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
兩種方式,一種是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);elsepagingResult = 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");elsenext.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");elsenext.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");elsenext.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");elsenext.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里
總結
以上是生活随笔為你收集整理的Java 分页,两种方式的分页,即取即用的代码,不客气的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu16安装CUDA9.0+An
- 下一篇: 初试牛客网PAT练习之数素数