美文网首页
分页查询返回数据

分页查询返回数据

作者: 墨色尘埃 | 来源:发表于2018-02-04 21:29 被阅读10次
public static Map convertFormData(Map mapData) {
        String page = (String) mapData.get("page");
        String rows = (String) mapData.get("rows");
        mapData.put("pageCopy", page);
        if ((page != null && !"".equals(page)) & (rows != null && !"".equals(rows))) {
            Integer intPage = Integer.valueOf(page);
            Integer intRows = Integer.valueOf(rows);
            int i = (intPage - 1) * intRows;
            mapData.put("page", i);
        }
        if (rows != null && !"".equals(rows))
            mapData.put("rows", Integer.valueOf(rows));
        String sort = (String) mapData.get("sort");
        if (sort != null) {
            sort = getSortString(sort);
            mapData.put("sort", sort);
        } else {
            mapData.put("sort", " order by OPERATE_TIME desc");
        }
        return mapData;
    }

PageUtil

public class PageUtil<T> {
    private int totalRecord;// 总记录数
    private int pageRecord = 10;// 每页记录数

    private int currentPage;// 当前页码

    private int totalPage;// 总页数
    private int beginIndex;//起始记录下标
    private int endIndex;//截止记录下标
    private List<T> resultList;

    public PageUtil(int totalRow, int pageNo) {
        this.totalRecord = totalRow;
        this.currentPage = pageNo;
        calculate();
    }

    public PageUtil(int totalRow, Map formData) {
        String pageCopy = (String) formData.get("pageCopy");
        if (!StringUtils.isEmpty(pageCopy)) {
            Integer page = Integer.valueOf(pageCopy);
//            if (page != null) page++;
            Integer rows = (Integer) formData.get("rows");
            if (page != null && rows != null) {
                this.totalRecord = totalRow;
                this.currentPage = page;
                this.pageRecord = rows;
            }
            calculate();
        }

    }

    public PageUtil(int totalRow, int pageNo, int pageSize) {
        this.totalRecord = totalRow;
        if (pageNo > 0) this.currentPage = pageNo;
        if (pageSize > 0) this.pageRecord = pageSize;
        calculate();
    }

    private void calculate() {
        totalPage = totalRecord / pageRecord + ((totalRecord % pageRecord) > 0 ? 1 : 0);

        if (currentPage > totalPage) {
            currentPage = totalPage;
        } else if (currentPage < 1) {
            currentPage = 1;
        }

        beginIndex = (currentPage - 1) * pageRecord;
        if (beginIndex < 0) beginIndex = 0;
        endIndex = beginIndex + pageRecord;
        if (endIndex > totalRecord) {
            endIndex = totalRecord;
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public int getPageRecord() {
        return pageRecord;
    }

    public int getBeginIndex() {
        return beginIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

}

相关文章

网友评论

      本文标题:分页查询返回数据

      本文链接:https://www.haomeiwen.com/subject/kyjvzxtx.html