提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.common.utils; |
H |
2 |
|
|
3 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
4 |
import com.baomidou.mybatisplus.core.metadata.OrderItem; |
|
5 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
6 |
import com.iailab.module.data.common.xss.SQLFilter; |
|
7 |
import org.apache.commons.lang3.StringUtils; |
|
8 |
|
|
9 |
import java.util.Map; |
|
10 |
|
|
11 |
/** |
|
12 |
* 查询参数 |
|
13 |
* |
|
14 |
* @author Mark sunlightcs@gmail.com |
|
15 |
*/ |
|
16 |
public class Query<T> { |
|
17 |
|
|
18 |
public IPage<T> getPage(Map<String, Object> params) { |
|
19 |
return this.getPage(params, null, false); |
|
20 |
} |
|
21 |
|
|
22 |
public IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) { |
|
23 |
//分页参数 |
|
24 |
long curPage = 1; |
|
25 |
long limit = 10; |
|
26 |
|
|
27 |
if(params.get(Constant.PAGE) != null){ |
|
28 |
curPage = Long.parseLong((String)params.get(Constant.PAGE)); |
|
29 |
} |
|
30 |
if(params.get(Constant.LIMIT) != null){ |
|
31 |
limit = Long.parseLong((String)params.get(Constant.LIMIT)); |
|
32 |
} |
|
33 |
|
|
34 |
//分页对象 |
|
35 |
Page<T> page = new Page<>(curPage, limit); |
|
36 |
|
|
37 |
//分页参数 |
|
38 |
params.put(Constant.PAGE, page); |
|
39 |
|
|
40 |
//排序字段 |
|
41 |
//防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险) |
|
42 |
String orderField = SQLFilter.sqlInject((String)params.get(Constant.ORDER_FIELD)); |
|
43 |
String order = (String)params.get(Constant.ORDER); |
|
44 |
|
|
45 |
|
|
46 |
//前端字段排序 |
|
47 |
if(StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)){ |
|
48 |
if(Constant.ASC.equalsIgnoreCase(order)) { |
|
49 |
return page.addOrder(OrderItem.asc(orderField)); |
|
50 |
}else { |
|
51 |
return page.addOrder(OrderItem.desc(orderField)); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
//没有排序字段,则不排序 |
|
56 |
if(StringUtils.isBlank(defaultOrderField)){ |
|
57 |
return page; |
|
58 |
} |
|
59 |
|
|
60 |
//默认排序 |
|
61 |
if(isAsc) { |
|
62 |
page.addOrder(OrderItem.asc(defaultOrderField)); |
|
63 |
}else { |
|
64 |
page.addOrder(OrderItem.desc(defaultOrderField)); |
|
65 |
} |
|
66 |
|
|
67 |
return page; |
|
68 |
} |
|
69 |
} |