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

com.starxmind.boot.pageable.mybatis.PageUtils Maven / Gradle / Ivy

package com.starxmind.boot.pageable.mybatis;

import com.github.pagehelper.PageInfo;
import com.starxmind.boot.pageable.core.PageResponse;

import java.util.List;

/**
 * 分页工具
 *
 * @author pizzalord
 * @since 1.0
 */
public abstract class PageUtils {
    /**
     * 获取分页结果
     *
     * @param list 一页数据list
     * @param   元素类型
     * @return
     */
    public static  PageResponse getPageResult(List list) {
        PageInfo pageInfo = new PageInfo(list);
        return new PageResponse().toBuilder()
                .pageNum(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .pages(pageInfo.getPages())
                .total(pageInfo.getTotal())
                .list(pageInfo.getList())
                .build();
    }

    public static int offset(int pageNum, int pageSize) {
        return (pageNum - 1) * pageSize;
    }

    /**
     * 计算总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public static int calcPages(long total, int pageSize) {
        long pages = total / pageSize;
        if (total % pageSize != 0) {
            pages++;
        }
        return (int) pages;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy