Newer
Older
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
package com.Config;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
@Intercepts(
{
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
}
)
public class CustomPlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey = null;
BoundSql boundSql = null;
Class<?> type = ms.getParameterMap().getType();
if (args.length >= 4) {
boundSql = ms.getBoundSql(parameter);
ResultHandler resultHandler = (ResultHandler) args[3];
RowBounds rowBounds = (RowBounds) args[2];
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
if (args.length == 6) {
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
String sql = getSql(boundSql, type);
//log.debug("runing query(dql) sql:{}", sql);
return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
} else if (args.length == 2) {
// update
boundSql = ms.getBoundSql(parameter);
String sql = getSql(boundSql, type);
//log.debug("runing update(dml) sql:{}", sql);
return executor.update(ms, parameter);
} else {
throw new RuntimeException("无效拦截");
}
}
private String getSql(BoundSql boundSql, Class<?> type) {
String sql = boundSql.getSql();
final String[] temp = {sql};
if (sql.contains("?") && boundSql.getParameterMappings().size() > 0 && boundSql.getParameterObject() != null) {
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
Object parameterObject = boundSql.getParameterObject();
//多参数
if (parameterObject instanceof HashMap) {
MapperMethod.ParamMap parameterObject1 = (MapperMethod.ParamMap) parameterObject;
parameterMappings.forEach(parameters -> {
String property = parameters.getProperty();
// todo 拦截过滤,大小过滤
String strs = String.valueOf(parameterObject1.get(property));
if (strs != null) {
temp[0] = temp[0].replaceFirst("\\?", "'" + strs + "'");
}
});
} else if (parameterObject instanceof String) {
// 单参数
parameterMappings.forEach(parameters -> {
// todo 拦截过滤,大小过滤
String strs = String.valueOf(parameterObject);
temp[0] = temp[0].replaceFirst("\\?", "'" + strs + "'");
});
} else if (parameterObject.getClass().isAssignableFrom(type)) {
// 对象类型
String ss = parameterObject.toString();
String substring = ss;
String[] strings = new String[1];
strings[0] = ss;
substring = substring.replaceFirst("\\(", "");
substring = substring.replaceFirst("\\)", "");
if (substring.contains("(") || substring.contains(")")) {
// 不管, 显示全部j对象数据
} else {
substring = strings[0];
substring = substring.replaceAll(" ", "");
HashMap<String, String> map = new HashMap<>();
int i = substring.indexOf("(");
int i1 = substring.lastIndexOf(")");
if (i != -1 && i1 != -1) {
substring = substring.substring(i + 1, i1);
}
// 若不存在复合类型的精准提供
String[] split = substring.split(",");
if (split.length > 0) {
for (String s : split) {
int i2 = s.indexOf("=");
String key = s.substring(0, i2);
String value = s.substring(i2 + 1);
map.put(key, value);
}
}
parameterMappings.forEach(parameters -> {
String property = parameters.getProperty();
String strs = String.valueOf(map.get(property));
temp[0] = temp[0].replaceFirst("\\?", "'" + strs + "'");
});
}
}
}
//TODO 自己要进行的各种处理
return temp[0];
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
//
}
}