All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.util.PageUtil Maven / Gradle / Ivy

The newest version!
package com.base4j.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 分页工具类
 *
 * @author xxx
 */
public class PageUtil {
    private final static Logger log = LoggerFactory.getLogger(PageUtil.class);

    /**
     * 将页数和每页条目数转换为开始位置和结束位置
* 此方法用于不包括结束位置的分页方法
* 例如:
* 页码:1,每页10 -> [0, 10]
* 页码:2,每页10 -> [10, 20]
* 。。。
* * @param pageNo 页码(从1计数) * @param countPerPage 每页条目数 * @return 第一个数为开始位置,第二个数为结束位置 */ public static int[] transToStartEnd(int pageNo, int countPerPage) { if (pageNo < 1) { pageNo = 1; } if (countPerPage < 1) { countPerPage = 0; log.warn("Count per page [{}] is not valid!", countPerPage); } int start = (pageNo - 1) * countPerPage; int end = start + countPerPage; return new int[]{start, end}; } /** * 根据总数计算总页数 * * @param totalCount 总数 * @param numPerPage 每页数 * @return 总页数 */ public static int totalPage(int totalCount, int numPerPage) { if (numPerPage == 0) { return 0; } return totalCount % numPerPage == 0 ? (totalCount / numPerPage) : (totalCount / numPerPage + 1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy