-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomizablePaginationInterceptor.java
262 lines (227 loc) · 11 KB
/
CustomizablePaginationInterceptor.java
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.yourcom.project.mybatis.interceptor;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.*;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* Date: 2023/2/14 17:37
* 重写了发出 "select count(*)"sql 的 autoCountSql 的方法, 可自定义总记录查询sql
* @author HuanyuMake
* @version 0.0.2
*/
@Configuration
// 定义从mybatis-plus.configuration.customizable-pagination-interceptor 前缀的application文件中读取属性配置该Bean
@ConfigurationProperties("mybatis-plus.configuration.customizable-pagination-interceptor")
public class CustomizablePaginationInterceptor extends PaginationInnerInterceptor {
protected String countField = "COUNT(1)";
protected String countFieldAlias = "total";
protected String countSqlSuffix = "_COUNT";
protected Boolean useMapperCountSql = false;
protected MappedStatement lastMs;
protected List<SelectItem> COUNT_SELECT_ITEM = Collections.singletonList(
(
new SelectExpressionItem(
(new Column()).withColumnName(countField)
)
).withAlias(new Alias(countFieldAlias))
); // 相当于 COUNT(1) AS total
public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
/*更新此次使用的MappedStatement,方便在 autoCountSql中使用*/
lastMs = ms;
return super.willDoQuery(executor , ms, parameter,rowBounds,resultHandler,boundSql);
}
@Override
protected String lowLevelCountSql(String originalSql) {
// 此处也使用自定义的总数查询sql
return String.format("SELECT %s AS %s FROM (%s) TOTAL", countField,countFieldAlias,originalSql);
}
@Override
protected String autoCountSql(@NotNull IPage<?> page, String sql) {
if(useMapperCountSql) {
// 尝试在mapper中查找是否有 countSqlSuffix 后缀的SQL,
MappedStatement ms = null;
try {
ms = lastMs.
getConfiguration()
.getMappedStatement(lastMs.getId() + countSqlSuffix);
} catch (Exception ignore){}
// 若找到对应的count statement
if(ms != null) {
// 若statement的返回值不是 java.lang.Long类型,则抛出 IllegalResultType 错误
if (ms.getResultMaps().get(0).getType() != Long.class) {
throw new IllegalResultType(
String.format("Statement '%s%s' return an illegal result type '%s', it must be '%s' type",
lastMs.getId(),countSqlSuffix, ms.getResultMaps().get(0).getType(), Long.class.getName()));
}
// 返回正确的statement对应的sql字符串, 形如 "SELECT COUNT(1) AS total FROM user"
return ms.getBoundSql(null)
.getSql();
}
}
/*
* 以下和 PaginationInnerInterceptor 父类的片段大体相同, 没有进行其它的逻辑处理,
* 之所以要抄过来是因为要使用本类的COUNT_SELECT_ITEM来充当查询总记录数的字段
*/
if (!page.optimizeCountSql()) {
return this.lowLevelCountSql(sql);
} else {
try {
Select select = (Select) CCJSqlParserUtil.parse(sql);
SelectBody selectBody = select.getSelectBody();
if (selectBody instanceof SetOperationList) {
return this.lowLevelCountSql(sql);
}
PlainSelect plainSelect = (PlainSelect) selectBody;
Distinct distinct = plainSelect.getDistinct();
GroupByElement groupBy = plainSelect.getGroupBy();
List<OrderByElement> orderBy = plainSelect.getOrderByElements();
if (CollectionUtils.isNotEmpty(orderBy)) {
boolean canClean = true;
if (groupBy != null) {
canClean = false;
}
if (canClean) {
for (OrderByElement order : orderBy) {
Expression expression = order.getExpression();
if (!(expression instanceof Column) && expression.toString().contains("?")) {
canClean = false;
break;
}
}
}
if (canClean) {
plainSelect.setOrderByElements((List) null);
}
}
for (SelectItem item : plainSelect.getSelectItems()) {
if (item.toString().contains("?")) {
return this.lowLevelCountSql(select.toString());
}
}
if (distinct == null && null == groupBy) {
if (this.optimizeJoin && page.optimizeJoinOfCountSql()) {
List<Join> joins = plainSelect.getJoins();
if (CollectionUtils.isNotEmpty(joins)) {
boolean canRemoveJoin = true;
String whereS = (String) Optional.ofNullable(plainSelect.getWhere()).map(Object::toString).orElse("");
whereS = whereS.toLowerCase();
for (Join join : joins) {
if (!join.isLeft()) {
canRemoveJoin = false;
break;
}
FromItem rightItem = join.getRightItem();
String str = "";
if (rightItem instanceof Table table) {
str = Optional.ofNullable(table.getAlias()).map(Alias::getName).orElse(table.getName()) + ".";
} else if (rightItem instanceof SubSelect subSelect) {
if (subSelect.toString().contains("?")) {
canRemoveJoin = false;
break;
}
str = subSelect.getAlias().getName() + ".";
}
str = str.toLowerCase();
if (whereS.contains(str)) {
canRemoveJoin = false;
break;
}
for (Expression expression : join.getOnExpressions()) {
if (expression.toString().contains("?")) {
canRemoveJoin = false;
break;
}
}
}
if (canRemoveJoin) {
plainSelect.setJoins((List) null);
}
}
}
// 使用本类的 COUNT_SELECT_ITEM 来充当查询字段
plainSelect.setSelectItems(COUNT_SELECT_ITEM);
return select.toString();
}
return this.lowLevelCountSql(select.toString());
} catch (JSQLParserException var18) {
this.logger.warn("optimize this sql to a count sql has exception, sql:\"" + sql + "\", exception:\n" + var18.getCause());
} catch (Exception var19) {
this.logger.warn("optimize this sql to a count sql has error, sql:\"" + sql + "\", exception:\n" + var19);
}
return this.lowLevelCountSql(sql);
}
}
public String getCountField() {
return countField;
}
public CustomizablePaginationInterceptor setCountField(String countField) {
this.countField = countField;
this.COUNT_SELECT_ITEM = generateSelectCountFields();
return this;
}
public String getCountFieldAlias() {
return countFieldAlias;
}
public CustomizablePaginationInterceptor setCountFieldAlias(String countFieldAlias) {
this.countFieldAlias = countFieldAlias;
this.COUNT_SELECT_ITEM = generateSelectCountFields();
return this;
}
public String getCountSqlSuffix() {
return countSqlSuffix;
}
public CustomizablePaginationInterceptor setCountSqlSuffix(String countSqlSuffix) {
this.countSqlSuffix = countSqlSuffix;
return this;
}
public Boolean getUseMapperCountSql() {
return useMapperCountSql;
}
public CustomizablePaginationInterceptor setUseMapperCountSql(Boolean useMapperCountSql) {
this.useMapperCountSql = useMapperCountSql;
return this;
}
protected List<SelectItem> generateSelectCountFields() {
return Collections.singletonList(
(
new SelectExpressionItem(
(new Column()).withColumnName(countField)
)
).withAlias(new Alias(countFieldAlias))
);
}
public static class IllegalResultType extends RuntimeException {
public IllegalResultType() {
}
public IllegalResultType(String message) {
super(message);
}
public IllegalResultType(String message, Throwable cause) {
super(message, cause);
}
public IllegalResultType(Throwable cause) {
super(cause);
}
public IllegalResultType(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
}