本文主要是介绍【java 分页工具类】分页工具PageBean(149),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分页工具类:
package com.itheima3;import java.util.ArrayList;
import java.util.List;public class PageBean<T> {private int pageSize; // 每页记录数private int pageNo; // 当前页private int totalPages; // 总页数private int totalRecords; // 总记录数private List<T> list;public int getPageSize() {return pageSize;}// 设置每页记录数public void setPageSize(int pageSize) {if (pageSize > 0) {this.pageSize = pageSize;} else {this.pageSize = 10;}}public int getPageNo() {return pageNo;}// 设置当前页public void setPageNo(int pageNo) {if (pageNo <= 1) {this.pageNo = 1;} else if (pageNo > this.totalPages) {this.pageNo = this.totalPages;} else {this.pageNo = pageNo;}}public int getTotalPages() {return totalPages;}// 设置总页数public void setTotalPages() {this.totalPages = this.totalRecords % this.pageSize == 0 ? this.totalRecords / this.pageSize: this.totalRecords / this.pageSize + 1;}public int getTotalRecords() {return totalRecords;}// 设置总记录数public void setTotalRecords(int totalRecords) {if (totalRecords >= 0) {this.totalRecords = totalRecords;} else {this.totalRecords = 0;}}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}// 当前页记录列表public void queryPager(int pageNo, int pageSize, List<T> list) {// 设置总记录数if (list != null && list.size() != 0) {this.setTotalRecords(list.size());} else {this.setTotalRecords(0);}// 设置每页记录数this.setPageSize(pageSize);// 设置总页数this.setTotalPages();// 设置当前页this.setPageNo(pageNo);// 设置当前页记录数if (list != null && list.size() != 0) {if (pageNo == this.totalPages) {this.list = list.subList((pageNo - 1) * pageSize, this.totalRecords);} else {this.list = list.subList((pageNo - 1) * pageSize, pageNo * pageSize);}} else {this.list = new ArrayList<>();}}public boolean isPrePage() {if (this.pageNo > 1) {return true;}return false;}public boolean isNextPage() {if (this.pageNo < this.totalPages) {return true;}return false;}public int getPrePageNo() {if (isPrePage()) {return this.pageNo - 1;}return this.pageNo;}public int getNextPageNo() {if (isNextPage()) {return this.pageNo + 1;}return this.pageNo;}}
控制层:
public PageBean queryList(@RequestParam(value = "page", defaultValue = "1") Integer page,@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {// if(page == null){// page = 1;// }// if(pageSize == null){// pageSize = 10;// }List<User> list = userService.queryList(); // 数据库查询返回的List;if (CollectionUtils.isNotEmpty(list)) {PageBean<User> bean = new PageBean<>();bean.queryPager(page, pageSize, list);return bean;}return null;}
这篇关于【java 分页工具类】分页工具PageBean(149)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!