springboot 自定义分页

自定义分页

package com.foxlink.utils;

import java.util.HashMap;
import java.util.List;

/**
 * 自定义分页的数据
 */

public class ListPageUtil {

    /**
     *
     * @param list 数据
     * @param current 当前页码
     * @param size  每页大小
     * @return
     * @param <T>
     */
    public static <T> HashMap<String, Object> paginate(List<T> list, int current, int size) {
        if (list == null || list.isEmpty()) {
            return new HashMap<>();
        }
        // 计算起始索引
        int start = (current - 1) * size;
        if (start >= list.size()) {
            return new HashMap<>();
        }
        // 计算结束索引
        int end = Math.min(start + size, list.size());
        List<T> records = list.subList(start, end);
        int total = list.size();
        int pages = (int) Math.ceil((double) total / size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        map.put("current", current);
        map.put("size", size);
        map.put("pages", pages);
        return map;
    }
}

 

posted @ 2025-06-09 15:41  市丸银  阅读(8)  评论(0)    收藏  举报