当前位置: 首页 > 工具软件 > ListPage > 使用案例 >

JPA List转成Page方法

慕迪
2023-12-01

List转成Page

当对非直接查询数据库得到的List,想转成通过JPA查询的Page分页列表,可以使用这个方法:

代码实现:

import org.fluttercode.datafactory.impl.DataFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.support.PageableExecutionUtils;

import java.lang.reflect.Modifier;
import java.util.*;

public class DataUtils {
/**
     * pageSize 每页数量以pageable中为准
     * page 从0开始
     *
     * @param list
     * @param pageable
     * @param <T>
     * @return
     */
    public static <T> Page<T> listToPage(List<T> list, Pageable pageable) {
        return getPage(list, pageable);
    }
    
    /**
     * pageSize 每页数量为15
     * page 从0开始
     *
     * @param list
     * @param page
     * @param <T>
     * @return
     */
    public static <T> Page<T> listToPage(List<T> list, int page) {
        PageRequest pageable = PageRequest.of(page, 15);
        return getPage(list, pageable);
    }

    private static <T> Page<T> getPage(List<T> list, Pageable pageable) {
        //第n页起始值
        int pageStart = pageable.getPageNumber() * pageable.getPageSize();
        //第n页期望结尾值
        int expectPageEnd = pageStart + pageable.getPageSize() - 1;
        return Optional.of(list.size())
                .filter(size -> size >= pageStart)
                .map(size -> getRealSubList(pageStart, expectPageEnd, list, pageable))
                .orElseGet(() -> getEmptySubList(list, pageable));
    }

    private static <T> PageImpl<T> getRealSubList(int pageStart, int expectPageEnd, List<T> list, Pageable pageable) {
        int realPageEnd = (list.size() > expectPageEnd || list.size() == expectPageEnd) ? expectPageEnd + 1 : list.size();
        return new PageImpl<>(list.subList(pageStart, realPageEnd), pageable, list.size());
    }

    private static <T> PageImpl<T> getEmptySubList(List<T> list, Pageable pageable) {
        return new PageImpl<>(new ArrayList(), pageable, list.size());
    }
}

单元测试:

 	@Test
    public void listToPage() {
        List<Account> integers = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            Account account = new Account();
            account.setId(Long.valueOf(i));
            integers.add(account);
        }
        PageRequest page1 = PageRequest.of(0, 15);
        Page<Account> integersPage1 = DataUtils.listToPage(integers, page1);
        TestCase.assertTrue(14 == integersPage1.getContent().get(14).getId());

        PageRequest page2 = PageRequest.of(1, 15);
        Page<Account> integersPage2 = DataUtils.listToPage(integers, page2);
        TestCase.assertTrue(29 == integersPage2.getContent().get(14).getId());

        PageRequest page3 = PageRequest.of(2, 15);
        Page<Account> integersPage3 = DataUtils.listToPage(integers, page3);
        TestCase.assertTrue(39 == integersPage3.getContent().get(9).getId());

        PageRequest page4 = PageRequest.of(3, 15);
        Page<Account> integersPage4 = DataUtils.listToPage(integers, page4);
        TestCase.assertTrue(0 == integersPage4.getContent().size());
    }
 类似资料: