Skip to content

Commit 89ffc97

Browse files
juzi214032colorful3
authored andcommitted
refactor(Page): 重构 Page 分页对象
采用继承的方式重构 Page 分页对象,减少无用代码 1.解决重写了过时方法的问题 2.修复“因没有修改 hashNext()方法”而可能导致的 bug
1 parent b288365 commit 89ffc97

1 file changed

Lines changed: 21 additions & 293 deletions

File tree

  • src/main/java/io/github/talelin/latticy/common/mybatis
Lines changed: 21 additions & 293 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,22 @@
11
package io.github.talelin.latticy.common.mybatis;
22

3-
import com.baomidou.mybatisplus.core.metadata.IPage;
4-
import com.baomidou.mybatisplus.core.metadata.OrderItem;
5-
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
6-
7-
import java.util.ArrayList;
8-
import java.util.Arrays;
9-
import java.util.Collections;
10-
import java.util.List;
11-
import java.util.function.Predicate;
12-
133
/**
14-
* 简单分页模型
15-
* mybatis-plus的默认分页实现,起始页都是1,为了与其它端保持一致,故重写了Page,起始页从 0 开始
4+
* 为和其他端保持一致
5+
* 重写 MyBatis-Plus 分页对象,将起始页从 1 改为 0
166
*
17-
* @author hubin
18-
* @since 2018-06-09
7+
* @author Juzi@TaleLin
198
*/
20-
public class Page<T> implements IPage<T> {
9+
public class Page<T> extends com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> {
2110

22-
private static final long serialVersionUID = 8545996863226528798L;
23-
24-
/**
25-
* 查询数据列表
26-
*/
27-
private List<T> records = Collections.emptyList();
11+
private static final long serialVersionUID = -2183463672525305273L;
2812

2913
/**
30-
* 总数
31-
*/
32-
private long total = 0;
33-
/**
34-
* 每页显示条数,默认 10
14+
* 该构造方法使得 current 总为 0
3515
*/
36-
private long size = 10;
37-
38-
/**
39-
* 当前页
40-
*/
41-
private long current = 0;
42-
43-
/**
44-
* 排序字段信息
45-
*/
46-
private List<OrderItem> orders = new ArrayList<>();
47-
48-
/**
49-
* 自动优化 COUNT SQL
50-
*/
51-
private boolean optimizeCountSql = true;
52-
/**
53-
* 是否进行 count 查询
54-
*/
55-
private boolean isSearchCount = true;
56-
5716
public Page() {
17+
super.setCurrent(0);
5818
}
5919

60-
/**
61-
* 分页构造函数
62-
*
63-
* @param current 当前页
64-
* @param size 每页显示条数
65-
*/
6620
public Page(long current, long size) {
6721
this(current, size, 0);
6822
}
@@ -75,255 +29,29 @@ public Page(long current, long size, boolean isSearchCount) {
7529
this(current, size, 0, isSearchCount);
7630
}
7731

78-
public Page(long current, long size, long total, boolean isSearchCount) {
79-
if (current > 0) {
80-
this.current = current;
81-
} else {
82-
// <= 0 页,就从0页开始
83-
this.current = 0;
84-
}
85-
this.size = size;
86-
this.total = total;
87-
this.isSearchCount = isSearchCount;
88-
}
89-
90-
/**
91-
* 是否存在上一页
92-
*
93-
* @return true / false
94-
*/
95-
public boolean hasPrevious() {
96-
return this.current > 0;
97-
}
98-
99-
/**
100-
* 是否存在下一页
101-
*
102-
* @return true / false
103-
*/
104-
public boolean hasNext() {
105-
return this.current < this.getPages();
106-
}
107-
108-
@Override
109-
public List<T> getRecords() {
110-
return this.records;
111-
}
112-
113-
@Override
114-
public Page<T> setRecords(List<T> records) {
115-
this.records = records;
116-
return this;
117-
}
118-
119-
@Override
120-
public long getTotal() {
121-
return this.total;
122-
}
123-
124-
@Override
125-
public Page<T> setTotal(long total) {
126-
this.total = total;
127-
return this;
128-
}
129-
130-
@Override
131-
public long getSize() {
132-
return this.size;
133-
}
134-
135-
@Override
136-
public Page<T> setSize(long size) {
137-
this.size = size;
138-
return this;
139-
}
140-
141-
@Override
142-
public long getCurrent() {
143-
return this.current;
144-
}
145-
146-
@Override
147-
public Page<T> setCurrent(long current) {
148-
this.current = current;
149-
return this;
150-
}
151-
152-
/**
153-
* 获取当前正序排列的字段集合
154-
* <p>
155-
* 为了兼容,将在不久后废弃
156-
*
157-
* @return 正序排列的字段集合
158-
* @see #getOrders()
159-
* @deprecated 3.1.2.2-SNAPSHOT
160-
*/
161-
@Override
162-
@Deprecated
163-
public String[] ascs() {
164-
return CollectionUtils.isNotEmpty(orders) ? mapOrderToArray(OrderItem::isAsc) : null;
165-
}
166-
167-
/**
168-
* 查找 order 中正序排序的字段数组
169-
*
170-
* @param filter 过滤器
171-
* @return 返回正序排列的字段数组
172-
*/
173-
private String[] mapOrderToArray(Predicate<OrderItem> filter) {
174-
List<String> columns = new ArrayList<>(orders.size());
175-
orders.forEach(i -> {
176-
if (filter.test(i)) {
177-
columns.add(i.getColumn());
178-
}
179-
});
180-
return columns.toArray(new String[0]);
181-
}
182-
183-
/**
184-
* 移除符合条件的条件
185-
*
186-
* @param filter 条件判断
187-
*/
188-
private void removeOrder(Predicate<OrderItem> filter) {
189-
for (int i = orders.size() - 1; i >= 0; i--) {
190-
if (filter.test(orders.get(i))) {
191-
orders.remove(i);
192-
}
193-
}
194-
}
195-
196-
/**
197-
* @param items 条件
198-
* @return 返回分页参数本身
199-
*/
200-
public Page<T> addOrder(OrderItem... items) {
201-
orders.addAll(Arrays.asList(items));
202-
return this;
203-
}
204-
205-
/**
206-
* 设置需要进行正序排序的字段
207-
* <p>
208-
* Replaced:{@link #addOrder(OrderItem...)}
209-
*
210-
* @param ascs 字段
211-
* @return 返回自身
212-
* @deprecated 3.1.2.2-SNAPSHOT
213-
*/
214-
@Deprecated
215-
public Page<T> setAscs(List<String> ascs) {
216-
return CollectionUtils.isNotEmpty(ascs) ? setAsc(ascs.toArray(new String[0])) : this;
217-
}
218-
219-
/**
220-
* 升序
221-
* <p>
222-
* Replaced:{@link #addOrder(OrderItem...)}
223-
*
224-
* @param ascs 多个升序字段
225-
* @deprecated 3.1.2.2-SNAPSHOT
226-
*/
227-
@Deprecated
228-
public Page<T> setAsc(String... ascs) {
229-
// 保证原来方法 set 的语意
230-
removeOrder(OrderItem::isAsc);
231-
for (String s : ascs) {
232-
addOrder(OrderItem.asc(s));
233-
}
234-
return this;
235-
}
236-
23732
/**
238-
* 获取需简要倒序排列的字段数组
239-
* <p>
240-
*
241-
* @return 倒序排列的字段数组
242-
* @see #getOrders()
243-
* @deprecated 3.1.2.2-SNAPSHOT
33+
* 该构造方法将小于 0 的 current 置为 0
34+
* @param current 当前页
35+
* @param size 每页显示条数,默认 10
36+
* @param total 总数
37+
* @param isSearchCount 是否进行 count 查询
24438
*/
245-
@Override
246-
@Deprecated
247-
public String[] descs() {
248-
return mapOrderToArray(i -> !i.isAsc());
249-
}
39+
public Page(long current, long size, long total, boolean isSearchCount) {
40+
super(current, size, total, isSearchCount);
25041

251-
/**
252-
* Replaced:{@link #addOrder(OrderItem...)}
253-
*
254-
* @param descs 需要倒序排列的字段
255-
* @return 自身
256-
* @deprecated 3.1.2.2-SNAPSHOT
257-
*/
258-
@Deprecated
259-
public Page<T> setDescs(List<String> descs) {
260-
// 保证原来方法 set 的语意
261-
if (CollectionUtils.isNotEmpty(descs)) {
262-
removeOrder(item -> !item.isAsc());
263-
for (String s : descs) {
264-
addOrder(OrderItem.desc(s));
265-
}
42+
if (current < 0) {
43+
current = 0;
26644
}
267-
return this;
268-
}
269-
270-
/**
271-
* 降序,这方法名不知道是谁起的
272-
* <p>
273-
* Replaced:{@link #addOrder(OrderItem...)}
274-
*
275-
* @param descs 多个降序字段
276-
* @deprecated 3.1.2.2-SNAPSHOT
277-
*/
278-
@Deprecated
279-
public Page<T> setDesc(String... descs) {
280-
setDescs(Arrays.asList(descs));
281-
return this;
45+
super.setCurrent(current);
28246
}
28347

28448
@Override
285-
public List<OrderItem> orders() {
286-
return getOrders();
287-
}
288-
289-
public List<OrderItem> getOrders() {
290-
return orders;
291-
}
292-
293-
public void setOrders(List<OrderItem> orders) {
294-
this.orders = orders;
295-
}
296-
297-
@Override
298-
public boolean optimizeCountSql() {
299-
return optimizeCountSql;
300-
}
301-
302-
@Override
303-
public boolean isSearchCount() {
304-
if (total < 0) {
305-
return false;
306-
}
307-
return isSearchCount;
308-
}
309-
310-
public Page<T> setSearchCount(boolean isSearchCount) {
311-
this.isSearchCount = isSearchCount;
312-
return this;
313-
}
314-
315-
public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
316-
this.optimizeCountSql = optimizeCountSql;
317-
return this;
49+
public boolean hasPrevious() {
50+
return super.getCurrent() > 0;
31851
}
31952

320-
/**
321-
* 重写计算偏移量,将分页从第 0 开始
322-
*
323-
* @return 偏移量
324-
*/
32553
@Override
326-
public long offset() {
327-
return getCurrent() * getSize();
54+
public boolean hasNext() {
55+
return super.getCurrent() + 1 < this.getPages();
32856
}
32957
}

0 commit comments

Comments
 (0)