分页工具类封装

分页工具类封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
​```java
package com.ujiuye.utils;

//分页工具类
public class PageTool {
private int currentPage;//当前页
private int pageSize;//每页显示的条数 自定义大小
private int startIndex;//起始索引 startIndex = (currentPage-1)*pageSize
private int totalCount;//总条数 查询数据库得到select count(*) from 表名;
private int totalPage;//总页数 计算: (totalCount%pageSize == 0) ? (totalCount/pageSize):(totalCount/pageSize+1)
private int prePage;//上一页 currentPage-1
private int nextPage;//下一页 currentPage+1

public PageTool(String currentPage, int totalCount) {
this.totalCount = totalCount;
//自定义显示的条数
this.pageSize = 3;
initCurrentPage(currentPage);//当前页
initTotalPage();
initStartIndex();
initPrePage();
initNextPage();
}

//给当前页进行初始化
private void initCurrentPage(String currentPage) {
//判断
if(currentPage == null) {
this.currentPage = 1;
}else {
//赋值第几页
this.currentPage = Integer.valueOf(currentPage);
}
}

//计算总页数
private void initTotalPage() {
//计算
this.totalPage = (totalCount%pageSize == 0) ? (totalCount/pageSize):(totalCount/pageSize+1);
}

//上一页
private void initPrePage() {
//判断
if(currentPage == 1) {
//赋值第一页
this.prePage = 1;
}else {
this.prePage = currentPage - 1;//2
}
}

//下一页
private void initNextPage() {
//判断
if(currentPage == totalPage) {
//赋值第一页
this.nextPage = totalPage;
}else {
this.nextPage = currentPage + 1;
}
}

//起始索引
private void initStartIndex() {
//计算
this.startIndex = (currentPage-1) * pageSize;
}

public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
}

​```

-------------本文结束感谢您的阅读-------------