-
Notifications
You must be signed in to change notification settings - Fork 42
/
BaseMapperAspect.java
330 lines (268 loc) · 11.6 KB
/
BaseMapperAspect.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package com.example.demo.trigger.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
@Aspect
public class BaseMapperAspect {
//切入点表达式,路径一定要写正确了
@Pointcut("execution( * com.example.demo.mapper.BaseMapper.*(..))")
public void access() {
}
//环绕增强,是在before前就会触发
@Around("access()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("-aop BaseMapperAspect环绕阶段-" + new Date());
String METHOD = pjp.getSignature().getName();
System.out.println("METHOD = " + METHOD);
Object[] args = pjp.getArgs();
Object o = args[0];
if (o.getClass().getName().equals("java.lang.String")){
// if(METHOD.equals("insertForID") || METHOD.equals("call")){
// //args = nullFinderBuilderInsertForID(args);
// }else{
// args = nullFinderBuilder(args);
// }
String sql = (String) o;
if (METHOD.equals("select") || METHOD.equals("count") || METHOD.equals("update") || METHOD.equals("insert") || METHOD.equals("insertForID")) {
String _sql = sql.replace("count(*)","*");
// if (_sql.contains("(") || _sql.contains(")") || _sql.contains(" or ") || _sql.contains(" OR ") ||
// _sql.contains(" between ") || _sql.contains(" BETWEEN ") ||
// _sql.contains(" is ") || _sql.contains(" IS ") ||
// _sql.contains(" in ") || _sql.contains(" IN ")) {
// System.out.println("sql = " + sql);
// System.out.println("复杂SQL不进行空值过滤");
// }
//
if (1==2) {
System.out.println("sql = " + sql);
System.out.println("复杂SQL不进行空值过滤");
} else {
//args = nullFinderBuilder(args);
if (METHOD.equals("insertForID")) {
args = nullFinderBuilderInsertForID(args);
} else {
args = nullFinderBuilder(args);
}
//o = args[0];
sql = (String) args[0];
System.out.println("sql = " + sql);
sql = nullFilterBuilder(METHOD, sql);
System.out.println("sql = " + sql);
}
}
sql = paramReplace(sql);
args[0]= sql;
}else{
List<String> sql = (List<String>) o;
sql = paramReplace(sql);
args[0]= sql;
}
Object result = pjp.proceed(args);
System.out.println("result = " + result);
if(result == null){
if(METHOD.equals("get")) {
return new LinkedHashMap<String,Object>();
}else if(METHOD.equals("count")){
return Long.valueOf(0);
}
}
System.out.println("result.getClass().getName() = " + result.getClass().getName());
if (result.getClass().getName().equals("java.util.ArrayList")){
formatTimeOfListMap((List<LinkedHashMap<String, Object>>) result);
}else if(result.getClass().getName().equals("java.util.LinkedHashMap")) {
formatTimeOfObjectMap((LinkedHashMap<String, Object>) result);
}else{
}
return result;
}
private int getKeyStringCount(String str, String key) {//方法
int count = 0;
int index = 0;
while((index = str.indexOf(key,index))!=-1){
index = index + key.length();
count++;
}
return count;
}
private String paramReplace(String param) {
int mun = getKeyStringCount(param,"?");
//System.out.println("mun = " + mun);
for (int i = 0; i < mun; i++) {
param = param.replaceFirst("\\?","#{args["+i+"]}");
//System.out.println("param = " + param);
}
return param;
}
private List<String> paramReplace(List<String> paramList) {
int total = 0;
for (int j = 0; j < paramList.size(); j++) {
String param = paramList.get(j);
int mun = getKeyStringCount(param,"?");
//System.out.println("mun = " + mun);
for (int i = total; i < mun+total; i++) {
param = param.replaceFirst("\\?","#{args["+i+"]}");
//System.out.println("param = " + param);
}
total = total+mun;
paramList.set(j,param);
}
return paramList;
}
private String nullFilterBuilder(String METHOD,String sqlStr) {
if(METHOD.equals("select") || METHOD.equals("count")) {
//清洗查询SQL中的空条件
//sqlStr = sqlStr.replaceAll("\\s+\\)", ")");
sqlStr = sqlStr + " ";
sqlStr = sqlStr.replaceAll("\\s*and\\s+\\w[-\\w.+]*\\s*(=|>|<|>=|<=|<>|!=)\\s*null\\s+(?!\\))", " ");
sqlStr = sqlStr.replaceAll("\\s*and\\s+\\w[-\\w.+]*\\s*(=|>|<|>=|<=|<>|!=)\\s*'null'\\s+(?!\\))", " ");
sqlStr = sqlStr.replaceAll("\\s*and\\s+\\w[-\\w.+]*\\s*like\\s*'%null%'\\s+(?!\\))", " ");
sqlStr = sqlStr.replaceAll("\\s+", " ");
sqlStr = sqlStr.trim();
}
if(METHOD.equals("update")) {
//清洗更新SQL中的空值
sqlStr = sqlStr.replaceAll("\\w[-\\w.+]*\\s*=\\s*null\\s*,?", "");
sqlStr = sqlStr.replaceAll("\\w[-\\w.+]*\\s*=\\s*'null'\\s*,?", "");
sqlStr = sqlStr.replaceAll(",\\s*where", " where");
}
if(METHOD.equals("insert") || METHOD.equals("insertForID")) {
//清洗插入SQL中的空值
// System.out.println("1111METHOD = " + METHOD);
//System.out.println("sqlStr = " + sqlStr);
sqlStr = nullFilterForInsert(sqlStr);
//System.out.println("sqlStr = " + sqlStr);
}
return sqlStr;
}
private Object[] nullFinderBuilder(Object[] args) {
Object o = args[0];
String sql = (String) o;
Object[] argsP = (Object[]) args[1];
int j=0;
for(int i=0;i<argsP.length;i++){
if(argsP[i]==null){
sql = sql.replaceFirst("\\?","null");
j++;
}else if("%null%".equals(argsP[i])){
sql = sql.replaceFirst("like\\s+\\?","=null");
argsP[i]=null;
j++;
}else{
sql = sql.replaceFirst("\\?","~=~=~=~");
}
}
if(j>0){
//System.out.println("清洗空值" + j);
argsP = Arrays.stream(argsP).filter(x -> x != null).toArray();
}
//System.out.println("sql = " + sql);
sql = sql.replaceAll("~=~=~=~","?");
args[0]= sql;
args[1]= argsP;
return args;
}
private Object[] nullFinderBuilderInsertForID(Object[] args) {
Object o = args[0];
String sql = (String) o;
Object[] argsP = (Object[]) args[2];
int j=0;
for(int i=0;i<argsP.length;i++){
if(argsP[i]==null){
sql = sql.replaceFirst("\\?","null");
j++;
}else if("%null%".equals(argsP[i])){
sql = sql.replaceFirst("like\\s*\\?","=null");
argsP[i]=null;
j++;
}else{
sql = sql.replaceFirst("\\?","~=~=~=~");
}
}
if(j>0){
//System.out.println("清洗空值" + j);
argsP = Arrays.stream(argsP).filter(x -> x != null).toArray();
}
//System.out.println("sql = " + sql);
sql = sql.replaceAll("~=~=~=~","?");
args[0]= sql;
args[2]= argsP;
return args;
}
//////////mybitis数据库连接串serverTimezone=Asia/Shanghai,,,数据库设置set time_zone='+8:00';就没问题
private String timeStamp2DateString(Timestamp timeStamp) {
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//fm.setTimeZone(TimeZone.getTimeZone("UTC"));
return fm.format(timeStamp);
}
private String timeStamp2DateString(LocalDateTime localDateTime) {
Timestamp timeStamp = new Timestamp(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return fm.format(timeStamp);
}
private int formatTimeOfListMap(List<LinkedHashMap<String, Object>> result) {
int n = 0;
for (LinkedHashMap<String, Object> m : result) {
//System.out.println("m = " + m);
//if(m==null) continue;
n+=formatTimeOfObjectMap(m);
}
return n;
}
private int formatTimeOfObjectMap(LinkedHashMap<String, Object> result) {
int n = 0;
if(result==null) return 0;
for (String k : result.keySet()) {
//System.out.println(k + " : " + result.get(k));
//System.out.println(result.get(k).getClass().getName());
if (result.get(k) != null && "java.sql.Timestamp".equals(result.get(k).getClass().getName())) {
result.put(k, timeStamp2DateString((Timestamp) result.get(k)));
n++;
}
if (result.get(k) != null && "java.sql.Date".equals(result.get(k).getClass().getName())) {
result.put(k, result.get(k).toString());
n++;
}
if (result.get(k) != null && "java.sql.Time".equals(result.get(k).getClass().getName())) {
result.put(k, result.get(k).toString());
n++;
}
if (result.get(k) != null && "java.time.LocalDateTime".equals(result.get(k).getClass().getName())) {
result.put(k, timeStamp2DateString((LocalDateTime) result.get(k)));
n++;
}
}
return n;
}
public String nullFilterForInsert(String str) {
String[] list1= str.split("\\)\\,\\s*\\(");
String[] list2= str.split("\\)\\s+(values|VALUES)");
if(!(list1.length ==1 && list2.length==2)){return str;}
String[] strOldList = str.trim().split("[(|)]",-1);
if(strOldList.length!=5){return str;}
String[] columnList = strOldList[1].trim().split("\\s*\\,\\s*");
String[] valuesList = strOldList[3].trim().split("\\s*\\,\\s*");
if(columnList.length != valuesList.length){return str;}
String columnsNew = "";
String valuesNew = "";
for (int i = 0; i < columnList.length; i++) {
if(!valuesList[i].equals("null")){
columnsNew+=columnList[i]+",";
valuesNew+=valuesList[i]+",";
}
}
columnsNew = columnsNew.substring(0, columnsNew.length() - 1);
valuesNew = valuesNew.substring(0, valuesNew.length() - 1);
String reStr = strOldList[0] + "("+columnsNew+") "+strOldList[2]+"("+valuesNew+")";
return reStr;
}
}