1.创建PageVo 实体类
package com.dh.steamapi;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* hhhh
* 分页实体类
* @param
*/
@Data
public class PageVo {
private int pageSize;//每页显示的元素条数
private int pageNo;//当前第几页 1是第一页
private int total;//总数
private List list;//分页结果内容
//实现分页方法
//skip+limit 可以实现分页 *** 作
public PageVo listPage(List dataList,int pageNo,int pageSize){
//pageNo 当前第几页 1是第一页
//pageSize 每页显示的元素条数
PageVo p = new PageVo();
List resultListPage = new ArrayList<>();
//skip(代表跳出前面多少元素) limit(每页数据)
resultListPage = dataList.stream().skip((pageNo-1) *
pageSize).limit(pageSize).collect(Collectors.toList());
p.setTotal(dataList.size());
p.setPageNo(pageNo);
p.setPageSize(pageSize);
p.setList(resultListPage);
//resultListPage.forEach(System.out::println);
return p;
}
}
2.用测试数据测试
package com.dh.steamapi;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestStreamAPI2{
//skip+limit 可以实现分页 *** 作
//测试数据
@Test
public void test2(){
List employeeList = new ArrayList<>();
employeeList.add(new Employee("李四","男",30,5000));
employeeList.add(new Employee("张三","男",38,2000));
employeeList.add(new Employee("王五","男",38,3000));
employeeList.add(new Employee("赵六","男",30,5000));
employeeList.add(new Employee("田七","男",38,3000));
employeeList.add(new Employee("重八","男",30,6000));
employeeList.add(new Employee("重八","男",30,6000));
employeeList.add(new Employee("重八","男",30,6000));
employeeList.add(new Employee("重八","男",30,6000));
PageVo pageVo = new PageVo<>();
List employeePageList = pageVo.listPage(employeeList, 2, 5).getList();
employeePageList.forEach(System.out::println);
}
}
3.效果图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)