JAVA常用的分页方式

常见的分页方式

1、guava分页

  • 引入maven依赖

    
    	com.google.guava
    	guava
    	21.0
    
    
  • 示例1

    @Test
    public void test() {
        // 准备数据
        ArrayList<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    
        // 将集合list按照 2 个元素一份进行划分
        List<List<Integer>> partition = Lists.partition(list, 2);
        for (List<Integer> nums : partition) {
          // 你需要处理的业务逻辑
          System.out.println(nums);
        }
    }
    
  • 示例2

    private List partition(ArrayList list, int pageSize, int pageNum) {
        // 将 List 按照 PageSzie 拆分
        List> partition = Lists.partition(list, pageSize);
        // 总页数
        int pages = partition.size();
        pageNum = pageNum <= 0 ? 0 : (pageNum <= (pages - 1) ? pageNum : (pages - 1));
        return partition.get(pageNum);
    }
    
    public void partitionTest() {
        int pageSize = 4; // 每一页显示的数量
        int pageNum = 3; // 页码
     
        // 准备数据
        ArrayList list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            
        System.out.println(partition(list, pageSize, -1));
        System.out.println(partition(list, pageSize, 0));
        System.out.println(partition(list, pageSize, 1));
        System.out.println(partition(list, pageSize, 4));
    }
    

2、MyBatis 分页插件 PageHelper

  • 引入pom

    
        com.github.pagehelper
        pagehelper
        5.0.1
    
    
  • 示例

    import java.util.ArrayList;
    import java.util.List;
     
    import com.github.pagehelper.Page;
    import com.github.pagehelper.PageInfo;
     
    public class TestListPage {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                list.add(i + 1 + "");
            }
            int currentPage = 1;
            int pageSize = 3;
           
            PageInfo<String> queryPageInfo = queryPageInfo(currentPage, pageSize, list);
            System.out.println("总条数:" + queryPageInfo.getTotal());
            System.out.print("数据:");
            queryPageInfo.getList().forEach(e -> System.out.print(e));
            System.out.println("当前页:" + queryPageInfo.getPageNum());
            System.out.println("每页个数:" + queryPageInfo.getPageSize());
            System.out.println("总页数:" + queryPageInfo.getPages());
        }
     
        /**
         * 处理List集合数据进行分页
         *
         * @param currentPage 当前页
         * @param pageSize    每页数据个数
         * @param list        进行分页的数据
         * @param 
         * @return
         */
        public static <T> PageInfo<T> queryPageInfo(int currentPage, int pageSize, List<T> list) {
            int total = list.size();
            if (total > pageSize) {
                int toIndex = pageSize * currentPage;
                if (toIndex > total) {
                    toIndex = total;
                }
                list = list.subList(pageSize * (currentPage - 1), toIndex);
            }
            Page<T> page = new Page<>(currentPage, pageSize);
            page.addAll(list);
            page.setPages((total + pageSize - 1) / pageSize);
            page.setTotal(total);
     
            PageInfo<T> pageInfo = new PageInfo<>(page);
            return pageInfo;
        }
     
    }
    

希望对你有所帮助~

你可能感兴趣的:(JAVA,java)