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);
            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里

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值