分页一
public Result<T> getList (@RequestParam(value = "page" defaultValue = "1") Integer page,@RequestParam(value = "limit" defaultValue = "10") Integer size,data){
List<T> result = Service.getList(data);
DPage<T> dPage = new DPage<>(result ,page,size);
return new Result(code:"0",msg:"success",dPage );
}
public class DPage<T>{
private Integer pageNum;
private Integer total;
private Integer pages;
@JsonIgnore
private Integer pageSize;
private List<T>list;
public DPAge(List<T> list,Integer pageNum,Integer pageSize){
this.pageNum = pageNum;
this.pageSize= pageSize;
this.total= list.size;
boolean full = total % pageSize == 0;
if(!full){
this.pages = total / pageSize + 1;
}else{
this.pages = total / pageSize;
}
int fromIndex = 0;
int toIndex = 0;
fromIndex = pageNum * pageSize - pageSize ;
if(pageNum == 0){
throw new ArithmeticException("第0页无法展示");
}else if(pageNum>pages){
list = new ArrayList<>();
}else if(pageNum==pages){
toIndex = total;
}else{
toIndex = pageNum * pageSize;
}
if(list.size() == 0){
this.list = list;
}else{
this.list = list.subList(fromIndex,toIndex);
}
}
}
分页二
public Result<T> getList (@RequestParam(value = "page" defaultValue = "1") Integer page,@RequestParam(value = "limit" defaultValue = "10") Integer size,data){
Page<T> result = Service.getList(result ,page,size);
return new Result(code:"0",msg:"success",result );
}
public class Page<T> implements Serializable {
private Integer allRow = 0;
private Integer currentPage= 1;
private Integer size= 10;
private Integer totalPage;
private List<T> dataList;
public Page(List<T> dataList, Integer allRow,Integer currentPage,Integer size){
this.allRow = allRow;
this.currentPage= currentPage;
this.size= size;
this.dataList= dataList;
if(allRow == 0 ){
this.totalPage = 1;
}else if(allRow % size == 0){
this.totalPage = allRow / size;
}else{
this.totalPage = allRow / size + 1;
}
public Integer getCurrentPage(){
if(currentPage < 1) return 1;
return currentPage;
}
public Integer getStart(){
return this.getSize() * (this.getCurrentPage() - 1 );
}
}
}