标签:div 限制 ack ati 文章 stat art 结束 界面
1 对应表的实体类(方便测试,只设置了一个字段)
1 package testDemo; 2 /** 3 * 实体类 4 * 5 * @author Dell 6 * 7 */ 8 public class Student { 9 private Integer id; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 }
2:分页的工具类(查询时只需查询出表中所有数据存入List集合中,不确定集合类型,工具类定义为Objec,转换方法在文章末,getInfoList()方法返回给界面需要展示的数据)
import java.util.ArrayList; import java.util.List; public class PageUtil { private Integer currentPage;//当前页数 private Integer pageSize;//页面大小 private Integer totalCount;//总数据量 private Integer totalPageCount;//总的页数 private List<Object> infoList;//接受的需要分页的数据 public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalCount() { return totalCount; } /** * 计算得到总页数 * * @param totalCount */ public void setTotalCount(Integer totalCount) { if (totalCount > 0) { this.totalCount = totalCount; totalPageCount = this.totalCount % pageSize == 0 ? (this.totalCount / pageSize) : (this.totalCount / pageSize + 1); } } public Integer getTotalPageCount() { return totalPageCount; } public void setTotalPageCount(Integer totalPageCount) { this.totalPageCount = totalPageCount; } public List<Object> getInfoList() { //临时数组,存储已经分页的数据集合 List<Object> tempList = new ArrayList<Object>(); //需要展示的第一条数据的索引 int startIndex = pageSize * (currentPage - 1); //结束的数据索引 int endIndex = (startIndex) + pageSize; for (int i = startIndex; i < endIndex; i++) { if (i < infoList.size()) { tempList.add(infoList.get(i)); } } return tempList; } public void setInfoList(List<Object> infoList) { this.infoList = infoList; } }
3:测试
1 public static void main(String[] args) { 2 3 // 模拟从数据中查询处的数据,20条 4 List<Student> stuList = new ArrayList<Student>(); 5 for (int i = 0; i < 20; i++) { 6 Student stu = new Student(); 7 stu.setId(i); 8 stuList.add(stu); 9 } 10 11 PageUtil pageUtil = new PageUtil(); 12 pageUtil.setPageSize(5);// 页面大小 13 pageUtil.setCurrentPage(2);// 当前页数 14 pageUtil.setTotalCount(stuList.size()); 15 // 传给工具类所有数据 16 pageUtil.setInfoList(Arrays.asList(stuList.toArray())); 17 18 // 临时集合,存储需要工具类返回的分页的数据集合 19 List<Student> showList = new ArrayList<Student>(); 20 showList = (List<Student>) (Object) pageUtil.getInfoList(); 21 // 模拟界面展示数据 22 for (int i = 0; i < showList.size(); i++) { 23 System.out.println(showList.get(i).getId()); 24 } 25 }
tips:
1 List(Object)集合转换为实体类集合
List<Student> showList = (List<Student>) (Object) Object集合 ;
2 List<实体类>集合转换为List集合
List<Object> objectList = Arrays.asList(stuList.toArray());
标签:div 限制 ack ati 文章 stat art 结束 界面
原文地址:https://www.cnblogs.com/ambition26/p/13376207.html