Springbo自定义分页工具类


import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.List;

public class PageUtil {
    /**
     * 分页
     * @param list
     * @param page
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> Page<T> page(List<T> list, int page, int pageSize) {
        int totalElements = list.size();
        int start = page * pageSize;
        int end = Math.min(start + pageSize, totalElements);

        return new PageImpl<>(list.subList(start, end),
                new PageRequest(page, pageSize),
                totalElements);
    }

    public static <T> Page<T> builidPage(List<T> list, long totalElements, int page, int pageSize) {
        return new PageImpl<>(list,
                new PageRequest(page, pageSize),
                totalElements);
    }

}

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
posted @ 2019-01-16 15:30  小漆同学  阅读(500)  评论(0编辑  收藏  举报