111
wang-hao-jie
2022-08-25 d90da0759bd93c7f429f6a20bc835782ad927c02
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 
package com.boying.common;
 
import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
 
public class Page<T> implements Serializable {
    
    private static final long serialVersionUID = 3412800354094709366L;
    public static final int DEFAULT_PAGE_SIZE=20;//默认每页数量
    public static final String ORDER_ASC = "asc";
    public static final String ORDER_DESC = "desc";
    /**
     * 字段排序映射,key:字段名,value:排序方向
     */
    private Map<String, String> orderMap = null;
    
    /** 当前页第一条数据的位置,从0开始 */
    private long page = 1;
 
    /** 每页的记录数 */
    private long pageSize = DEFAULT_PAGE_SIZE;
 
    /**
     * 是否自动请求总记录数
     */
    protected boolean autoCount = true;
 
    /**
     * 总纪录数,当autoCount为true时有效。
     * <p>
     * 默认值为-1
     */
    protected long total = -1;
 
    /**
     * 页内记录
     */
    protected List<T> rows;
 
    // -- 构造函数 --//
    public Page() {
        rows = Collections.emptyList();
    }
    public Page(int page, int pageSize) {
        this.page = page;
        this.pageSize = pageSize;
    }
    /**
     * 获得当前页的起始记录序号,序号从0开始,默认为0
     */
    public long getStartIndex() {
        return page < 1 ? 0 : (page - 1) * pageSize;
    }
 
    /**
     * 获得每页的记录数量,默认为20.
     */
    public long getPageSize() {
        return pageSize;
    }
 
    /**
     * 获得字段排序映射容器。
     * 
     * @return the orderMap
     */
    public Map<String, String> getOrderMap() {
        return orderMap;
    }
 
    /**
     * 添加一个字段的排序映射
     * 
     * @param propertyName
     * @param orderType
     */
    public void addOrder(String propertyName, String orderType) {
        if (orderMap == null)
            orderMap = new LinkedHashMap<String, String>();
        orderMap.put(propertyName, orderType);
    }
    /**
     * 添加一个字段的排序映射
     * 
     * @param propertyName
     */
    public void addAscOrder(String propertyName) {
        if (orderMap== null)
            orderMap = new LinkedHashMap<String, String>();
        orderMap.put(propertyName,ORDER_ASC);
    }
    /**
     * 添加一个字段的排序映射
     * 
     * @param propertyName
     */
    public void addDescOrder(String propertyName) {
        if (orderMap== null)
            orderMap = new LinkedHashMap<String, String>();
        orderMap.put(propertyName,ORDER_DESC);
    }
 
    /**
     * 是否进行排序
     * 
     * @return
     */
    public boolean isOrder() {
        return orderMap != null && !orderMap.isEmpty();
    }
 
    /**
     * 查询对象时是否自动另外执行count查询获取总记录数, 默认为false.
     */
    public boolean isAutoCount() {
        return autoCount;
    }
 
    /**
     * 查询对象时是否自动另外执行count查询获取总记录数.
     */
    public void setAutoCount(final boolean autoCount) {
        this.autoCount = autoCount;
    }
 
    /**
     * 通过是否进行总纪录数查询得到默认分页
     * 
     * @param autoCount
     * @return
     */
    public Page<T> autoCount(final boolean autoCount) {
        setAutoCount(autoCount);
        return this;
    }
 
    // -- 访问查询结果方法 --//
    /**
     * 取得页内的记录列表.
     */
    public List<T> getRows() {
        return rows;
    }
 
    /**
     * 设置页内的记录列表.
     */
    public void setRows(final List<T> rows) {
        this.rows = rows;
    }
 
    /**
     * 取得总记录数, 默认值为-1.
     */
    public long getTotal() {
        return total;
    }
 
    /**
     * 设置总记录数.
     */
    public void setTotal(final long total) {
        this.total = total;
    }
    /**
     * 根据页容量(pageSize)与总记录数(totalCount)计算总页数(totalPage), 默认值为-1.
     */
    public long getTotalPage() {
        if (total < 0) {
            return -1;
        }
        long count = total / getPageSize();
        if (total % getPageSize() > 0) {
            count++;
        }
        return count;
    }
    // --页操作方法 --//
    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return page <= this.getTotalPage() - 1;
    }
    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return (page - 1 >= 1);
    }
    /**
     * 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
     */
    public long getNextPage() {
        if (isHasNext()) {
            return page + 1;
        } else {
            return page;
        }
    }
    /**
     * 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
     */
    public long getPrePage() {
        if (isHasPre()) {
            return page - 1;
        } else {
            return page;
        }
    }
    public long getPage() {
        return page;
    }
    public void setPage(long page) {
        this.page = page;
    }
    
}