1.自定义分页实体类PageBean
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created on 2017/11/22.
* 分页实体类
*/
@Component
public class PageBean<T> {
private List<T> list;//数据集合
private Integer pageNum = 1;//当前页
private Integer prePageNum;//上一页码
private Integer nextPageNum;//下一页码
private Integer startNum = 0;//开始页码
private Integer endNum = 0;//结束页码
private Integer pageRows = 15;//每页记录数
private Integer startRowNum;//查询开始行
private Integer endRowNum;//查询结束行
private Integer totalRows;//总记录数
private Integer totalPages;//总页数
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPrePageNum() {
this.prePageNum = this.pageNum - 1;
if (this.prePageNum < 1) {
this.prePageNum = 1;
}
return prePageNum;
}
public void setPrePageNum(Integer prePageNum) {
this.prePageNum = prePageNum;
}
public Integer getNextPageNum() {
this.nextPageNum = this.pageNum + 1;
if (this.nextPageNum > this.getTotalPages()) {
this.nextPageNum = this.getTotalPages();
}
return nextPageNum;
}
public void setNextPageNum(Integer nextPageNum) {
this.nextPageNum = nextPageNum;
}
public Integer getStartNum() {
if (this.pageNum < 6) {
return 1;
} else {
if(this.getTotalPages() > 9){
if (pageNum == totalPages) {
return pageNum - 8;
} else {
if (this.getTotalPages() - this.pageNum < 5) {
return this.getTotalPages() - 8;
} else {
return pageNum - 4;
}
}
}else {
return 1;
}
}
}
public void setStartNum(Integer startNum) {
this.startNum = startNum;
}
public Integer getEndNum() {
if (this.pageNum < 6) {
if (this.getTotalPages() > 9) {
return 9;
} else {
return (this.getTotalPages()==0)?1:this.getTotalPages();
}
} else {
if (pageNum == this.getTotalPages()) {
return this.getTotalPages();
} else {
if (this.getTotalPages() - this.pageNum < 5) {
return this.getTotalPages();
} else {
return pageNum + 4;
}
}
}
}
public void setEndNum(Integer endNum) {
this.endNum = endNum;
}
public Integer getPageRows() {
return pageRows;
}
public void setPageRows(Integer pageRows) {
this.pageRows = pageRows;
}
public Integer getStartRowNum() {
return (pageNum - 1) * pageRows;
}
public void setStartRowNum(Integer startRowNum) {
this.startRowNum = startRowNum;
}
public Integer getEndRowNum() {
return pageRows;
}
public void setEndRowNum(Integer endRowNum) {
this.endRowNum = endRowNum;
}
public Integer getTotalRows() {
return totalRows;
}
public void setTotalRows(Integer totalRows) {
this.totalRows = totalRows;
}
public Integer getTotalPages() {
totalPages = getTotalRows() % pageRows == 0 ? totalRows / pageRows : totalRows / pageRows + 1;
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}
2.调用分页实体类
public PageBean selectActivityList(PageBean pb) {
//pb中的当前页、每页记录数需要从前台获取
Map<String,Object> pm = new HashMap<String,Object>();
//查询记录总数
int rows = selectDataCount(pm);
pb.setTotalRows(rows);
//查询记录集合
pm.put("pb",pb);
List<Object> list = null;
try {
list = selectDataList(pm);
}catch (Exception e){
e.printStackTrace();
}
pb.setList(list);
return pb;
}