-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathPageUtils.java
More file actions
133 lines (123 loc) · 3.92 KB
/
Copy pathPageUtils.java
File metadata and controls
133 lines (123 loc) · 3.92 KB
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
package com.kevinblandy.simple.webchat.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
/**
* 分页工具类
* @author KevinBlandy
* @version 1.0
* @date 2017年5月11日 下午8:59:01
*/
public class PageUtils {
/**
* 默认页码
*/
private static final Integer DEFAULT_PAGE = 1;
/**
* 默认每页显示数量
*/
private static final Integer DEFAULT_ROWS = 10;
/**
* 获取一个pageBounds,需要检索总记录数
* @param page
* @param rows
* @param orders
* @return
*/
public static PageBounds getPageBounds(Integer page, Integer rows, Order... orders){
return PageUtils.getPageBounds(page,rows,true,orders);
}
/**
* 获取一个pageBounds,不需要检索总记录数
* @param page
* @param rows
* @param orders
* @return
*/
public static PageBounds getPageBoundsNoneTotalCount(Integer page, Integer rows, Order... orders){
return PageUtils.getPageBounds(page,rows,false,orders);
}
/**
* 获取PageBounds
* @param page 页码
* @param rows 每页记录数
* @param totalCount 是否检索总记录数
* @param orders
* @return
*/
public static PageBounds getPageBounds(Integer page, Integer rows,boolean totalCount, Order... orders){
if(page == null || page < 1){
page = DEFAULT_PAGE;
}
if(rows == null || rows < 1){
rows = DEFAULT_ROWS;
}
PageBounds pageBounds = new PageBounds(page,rows);
if(!GeneralUtils.isEmpty(orders)){
//存在一个或者多个排序字段
pageBounds.setOrders(Arrays.asList(orders));
}
//是否检索总记录数
pageBounds.setContainsTotalCount(totalCount);
//禁止异步检索总记录数
pageBounds.setAsyncTotalCount(Boolean.FALSE);
return pageBounds;
}
/**
* 获取一组排序策略
* @param sorts 字段s
* @param orders 策略s(ASC/DESC)
* @return
*/
public static Order[] getOrders(String[] sorts,String[] orders){
List<Order> result = new ArrayList<Order>();
if(!GeneralUtils.isEmpty(sorts)){
//确定存在排序字段
if(GeneralUtils.isEmpty(orders)){
//没有任何排序策略,默认全部为DESC
for(String sort : sorts){
if(!GeneralUtils.isEmpty(sort)){
result.add(getOrderDesc(sort));
}
}
}else{
for(int x = 0 ;x < sorts.length ; x++){
if(GeneralUtils.isEmpty(sorts[x])){
continue;
}
if(x < orders.length){
if("ASC".equalsIgnoreCase(orders[x])){
//ASC
result.add(getOrderAsc(sorts[x]));
}else{
//DESC
result.add(getOrderDesc(sorts[x]));
}
}else{
//排序策略未定义部分,默认为DESC
result.add(getOrderDesc(sorts[x]));
}
}
}
}
return result.toArray(new Order[result.size()]);
}
/**
* 获取一个逆序的ORDER
* @param filed
* @return
*/
public static Order getOrderDesc(String filed){
return new Order(filed, Order.Direction.DESC,null);
}
/**
* 获取一个正序的ORDER
* @param filed
* @return
*/
public static Order getOrderAsc(String filed){
return new Order(filed, Order.Direction.ASC,null);
}
}