JAVA8的分页工具
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
使用JAVA8的API可以实现分页,在数据量相对稳定的情况下,可以查出所有数据,配合缓存使用
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Page {
private int current_page;
private int size;
private int total_page;
private int total_sum;
private transient List instanceList;//Gson不序列化transient字段
private List currentPageData;
private transient Optional> op;
public Page(List instanceList,int size) {
this.size = size;
setInstanceList(instanceList);
}
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page<1?1:current_page>this.total_page?this.total_page:current_page;
setCurrentPageData(currentPageData());
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotal_page() {
return total_page;
}
public int getTotal_sum() {
return total_sum;
}
public List getInstanceList() {
return instanceList;
}
public void setInstanceList(List instanceList) {
this.op= Optional.ofNullable(instanceList);
this.instanceList = op.orElse(new ArrayList());
this.total_sum = this.instanceList.size();
this.total_page =(int) Math.ceil(1.0*this.total_sum/this.size);
}
public void setCurrentPageData(List currentPageData) {
this.currentPageData = currentPageData;
}
public List getCurrentPageData(){
return this.currentPageData;
}
private List currentPageData(){
if(this.size==0 || this.total_page == 1){
return this.instanceList;
}
List currentPageData = new ArrayList();
instanceList.stream().skip((this.current_page-1)*this.size).limit(this.size).forEach(e->currentPageData.add(e));
return currentPageData;
}
}
用法:
List newsList = newsService.findAll();
Page page = new Page<>(newsList, size);
page.setCurrent_page(current_page);
return JsonUtil.toJson(page,"yyyy-MM-dd");
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341