-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleCriterionExtendPlugin.java
335 lines (288 loc) · 21.7 KB
/
ExampleCriterionExtendPlugin.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
331
332
333
334
335
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* 增强example Criterion,支持两个参数的函数
*/
public class ExampleCriterionExtendPlugin extends AbstractXmbgPlugin {
private static final Logger log = LoggerFactory.getLogger(ExampleCriterionExtendPlugin.class);
@Override
public boolean validate(List<String> list) {
return true;
}
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
List<InnerClass> innerClasses = topLevelClass.getInnerClasses();
for (InnerClass innerClass : innerClasses) {
if ("Criterion".equals(innerClass.getType().getShortName())) {
Field additionalCondition = new Field("additionalCondition", FullyQualifiedJavaType.getIntInstance());
additionalCondition.setVisibility(JavaVisibility.PRIVATE);
additionalCondition.setInitializationString("0");
innerClass.addField(additionalCondition);
Method getAdditionalCondition = new Method("getAdditionalCondition");
getAdditionalCondition.setVisibility(JavaVisibility.PUBLIC);
getAdditionalCondition.setReturnType(FullyQualifiedJavaType.getIntInstance());
getAdditionalCondition.addBodyLine("return additionalCondition;");
innerClass.addMethod(getAdditionalCondition);
Field thirdValue = new Field("thirdValue", FullyQualifiedJavaType.getObjectInstance());
thirdValue.setVisibility(JavaVisibility.PRIVATE);
innerClass.addField(thirdValue);
Method getThirdValue = new Method("getThirdValue");
getThirdValue.setVisibility(JavaVisibility.PUBLIC);
getThirdValue.setReturnType(FullyQualifiedJavaType.getObjectInstance());
getThirdValue.addBodyLine("return thirdValue;");
innerClass.addMethod(getThirdValue);
Parameter additionalConditionParameter = new Parameter(FullyQualifiedJavaType.getIntInstance(), "additionalCondition", false);
Parameter conditionParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition", false);
Parameter valueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value", false);
Parameter secondValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "secondValue", false);
Parameter thirdValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "thirdValue", false);
Method criterionConstruct = new Method("Criterion");
criterionConstruct.setConstructor(true);
criterionConstruct.setVisibility(JavaVisibility.PROTECTED);
criterionConstruct.addParameter(additionalConditionParameter);
criterionConstruct.addParameter(conditionParameter);
criterionConstruct.addParameter(valueParameter);
criterionConstruct.addParameter(secondValueParameter);
criterionConstruct.addBodyLine("super();");
criterionConstruct.addBodyLine("this.additionalCondition = additionalCondition;");
criterionConstruct.addBodyLine("this.condition = condition;");
criterionConstruct.addBodyLine("this.value = value;");
criterionConstruct.addBodyLine("this.secondValue = secondValue;");
innerClass.addMethod(criterionConstruct);
{
Method criterionThirdConstruct = new Method("Criterion");
criterionThirdConstruct.setConstructor(true);
criterionThirdConstruct.setVisibility(JavaVisibility.PROTECTED);
criterionThirdConstruct.addParameter(additionalConditionParameter);
criterionThirdConstruct.addParameter(conditionParameter);
criterionThirdConstruct.addParameter(valueParameter);
criterionThirdConstruct.addParameter(secondValueParameter);
criterionThirdConstruct.addParameter(thirdValueParameter);
criterionThirdConstruct.addBodyLine("super();");
criterionThirdConstruct.addBodyLine("this.additionalCondition = additionalCondition;");
criterionThirdConstruct.addBodyLine("this.condition = condition;");
criterionThirdConstruct.addBodyLine("this.value = value;");
criterionThirdConstruct.addBodyLine("this.secondValue = secondValue;");
criterionThirdConstruct.addBodyLine("this.thirdValue = thirdValue;");
innerClass.addMethod(criterionThirdConstruct);
}
} else if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) {
innerClass.getMethods().forEach(method -> {
//equalTo 当字段value为null时不抛出异常,跳过、忽略添加该条件
if ("addCriterion".equals(method.getName()) && method.getParameters().size() == 3) {
for (int j = 0; j < method.getBodyLines().size(); j++) {
if (method.getBodyLines().get(j).contains("throw")) {
method.getBodyLines().remove(j);
method.getBodyLines().add(j, "return;");
break;
}
}
}
// between and 当字段value为null时不抛出异常,跳过、忽略添加该条件
if ("addCriterion".equals(method.getName()) && method.getParameters().size() == 4) {
String types = "String_Object_Object_String_";
StringBuilder stringBuilder = new StringBuilder();
method.getParameters().forEach(e -> {
stringBuilder.append(e.getType().getShortName()).append("_");
});
if (types.contentEquals(stringBuilder)) {
for (int j = 0; j < method.getBodyLines().size(); j++) {
if (method.getBodyLines().get(j).contains("throw")) {
method.getBodyLines().remove(j);
method.getBodyLines().add(j, "return;");
break;
}
}
}
}
});
Method addCriterion = new Method("addCriterion");
addCriterion.setVisibility(JavaVisibility.PROTECTED);
Parameter additionalConditionParameter = new Parameter(FullyQualifiedJavaType.getIntInstance(), "additionalCondition", false);
Parameter functionNameParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "functionName", false);
Parameter valueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value", false);
Parameter secondValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "secondValue", false);
addCriterion.addParameter(additionalConditionParameter);
addCriterion.addParameter(functionNameParameter);
addCriterion.addParameter(valueParameter);
addCriterion.addParameter(secondValueParameter);
addCriterion.addBodyLine("criteria.add(new Criterion(additionalCondition, functionName, value, secondValue));");
innerClass.addMethod(addCriterion);
{
Method thirdAddCriterion = new Method("addCriterion");
thirdAddCriterion.setVisibility(JavaVisibility.PROTECTED);
Parameter thirdValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "thirdValue", false);
thirdAddCriterion.addParameter(additionalConditionParameter);
thirdAddCriterion.addParameter(functionNameParameter);
thirdAddCriterion.addParameter(valueParameter);
thirdAddCriterion.addParameter(secondValueParameter);
thirdAddCriterion.addParameter(thirdValueParameter);
thirdAddCriterion.addBodyLine("criteria.add(new Criterion(additionalCondition, functionName, value, secondValue, thirdValue));");
innerClass.addMethod(thirdAddCriterion);
}
Method andConditionValueMethod = new Method("andConditionValue");
andConditionValueMethod.setVisibility(JavaVisibility.PUBLIC);
Parameter searchConditionParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "searchCondition", false);
Parameter searchValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "searchValue", false);
andConditionValueMethod.addParameter(searchConditionParameter);
andConditionValueMethod.addParameter(searchValueParameter);
andConditionValueMethod.addBodyLine("addCriterion(searchCondition, searchValue, \"\");");
andConditionValueMethod.addBodyLine("return (Criteria) this;");
andConditionValueMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andConditionValueMethod);
{
Method andConditionJsonFieldValueMethod = new Method("andConditionJsonFieldValue");
andConditionJsonFieldValueMethod.setVisibility(JavaVisibility.PUBLIC);
Parameter jsonColumnConditionParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "jsonColumn", false);
Parameter jsonFieldValueParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "jsonField", false);
Parameter jsonConditionParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition", false);
Parameter jsonSearchValueParameter = new Parameter(FullyQualifiedJavaType.getObjectInstance(), "searchValue", false);
andConditionJsonFieldValueMethod.addParameter(jsonColumnConditionParameter);
andConditionJsonFieldValueMethod.addParameter(jsonFieldValueParameter);
andConditionJsonFieldValueMethod.addParameter(jsonConditionParameter);
andConditionJsonFieldValueMethod.addParameter(jsonSearchValueParameter);
andConditionJsonFieldValueMethod.addBodyLine("addCriterion(4, condition, jsonColumn, jsonField, searchValue);");
andConditionJsonFieldValueMethod.addBodyLine("return (Criteria) this;");
andConditionJsonFieldValueMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andConditionJsonFieldValueMethod);
Method andConditionJsonFieldContainsMethod = new Method("andConditionJsonFieldContains");
andConditionJsonFieldContainsMethod.setVisibility(JavaVisibility.PUBLIC);
andConditionJsonFieldContainsMethod.addParameter(jsonColumnConditionParameter);
andConditionJsonFieldContainsMethod.addParameter(jsonFieldValueParameter);
andConditionJsonFieldContainsMethod.addParameter(jsonSearchValueParameter);
andConditionJsonFieldContainsMethod.addBodyLine("addCriterion(7, \"JSON_CONTAINS\", jsonColumn, jsonField, searchValue);");
andConditionJsonFieldContainsMethod.addBodyLine("return (Criteria) this;");
andConditionJsonFieldContainsMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andConditionJsonFieldContainsMethod);
}
Method andFunctionLeftKeyMethod = new Method("andFunctionLeftKey");
andFunctionLeftKeyMethod.setVisibility(JavaVisibility.PUBLIC);
Parameter functionParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "functionName", false);
Parameter searchKeyParameter = new Parameter(FullyQualifiedJavaType.getStringInstance(), "searchKey", false);
andFunctionLeftKeyMethod.addParameter(functionParameter);
andFunctionLeftKeyMethod.addParameter(searchKeyParameter);
andFunctionLeftKeyMethod.addParameter(searchValueParameter);
andFunctionLeftKeyMethod.addBodyLine("addCriterion(5, functionName, searchKey, searchValue);");
andFunctionLeftKeyMethod.addBodyLine("return (Criteria) this;");
andFunctionLeftKeyMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andFunctionLeftKeyMethod);
Method andFunctionRightKeyMethod = new Method("andFunctionRightKey");
andFunctionRightKeyMethod.setVisibility(JavaVisibility.PUBLIC);
andFunctionRightKeyMethod.addParameter(functionParameter);
andFunctionRightKeyMethod.addParameter(searchKeyParameter);
andFunctionRightKeyMethod.addParameter(searchValueParameter);
andFunctionRightKeyMethod.addBodyLine("addCriterion(6, functionName, searchKey, searchValue);");
andFunctionRightKeyMethod.addBodyLine("return (Criteria) this;");
andFunctionRightKeyMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andFunctionRightKeyMethod);
introspectedTable.getAllColumns().forEach(introspectedColumn -> {
String javaProperty = introspectedColumn.getJavaProperty();
String columnName = introspectedColumn.getActualColumnName();
Parameter expValue = new Parameter(FullyQualifiedJavaType.getStringInstance(), "regexp", false);
Method andRegexpMethod = new Method("and" + upperCaseFirstChar(javaProperty) + "Regexp");
andRegexpMethod.setVisibility(JavaVisibility.PUBLIC);
andRegexpMethod.addParameter(expValue);
andRegexpMethod.addBodyLine("addCriterion(\"" + columnName + " regexp\", " + "regexp, \"" + javaProperty + "\");");
andRegexpMethod.addBodyLine("return (Criteria) this;");
andRegexpMethod.setReturnType(new FullyQualifiedJavaType("Criteria"));
innerClass.addMethod(andRegexpMethod);
});
}
}
return true;
}
@Override
public boolean sqlMapExampleWhereClauseElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
XmlElement chooseChild = findFirstMatchedXmlElement(element, "choose");
if (chooseChild != null) {
/*XmlElement keyValueElement = new XmlElement("when");
keyValueElement.addAttribute(new Attribute("test", "criterion.additionalCondition == 3"));
keyValueElement.addElement(new TextElement("and ${criterion.condition} #{criterion.value}"));
chooseChild.addElement(keyValueElement);*/
{
{
XmlElement jsonKeyValueElement = new XmlElement("when");
jsonKeyValueElement.addAttribute(new Attribute("test", "criterion.additionalCondition == 4"));
XmlElement jsonIfMysqlElement = new XmlElement("if");
jsonIfMysqlElement.addAttribute(new Attribute("test", "_databaseId == 'mysql'"));
jsonIfMysqlElement.addElement(new TextElement("and ${criterion.value}->>'$.${criterion.secondValue}' ${criterion.condition} #{criterion.thirdValue}"));
jsonKeyValueElement.addElement(jsonIfMysqlElement);
XmlElement jsonIfPostgresqlElement = new XmlElement("if");
jsonIfPostgresqlElement.addAttribute(new Attribute("test", "_databaseId == 'postgresql'"));
jsonIfPostgresqlElement.addElement(new TextElement("and ${criterion.value}->>'${criterion.secondValue}' ${criterion.condition} #{criterion.thirdValue}"));
jsonKeyValueElement.addElement(jsonIfPostgresqlElement);
XmlElement jsonIfNotMysqlElement = new XmlElement("if");
jsonIfNotMysqlElement.addAttribute(new Attribute("test", "_databaseId != 'mysql' and _databaseId != 'postgresql'"));
jsonIfNotMysqlElement.addElement(new TextElement("<!--"));
jsonIfNotMysqlElement.addElement(new TextElement(" otherwise."));
jsonIfNotMysqlElement.addElement(new TextElement("-->"));
jsonKeyValueElement.addElement(jsonIfNotMysqlElement);
chooseChild.addElement(jsonKeyValueElement);
}
{
XmlElement jsonKeyValueElement = new XmlElement("when");
jsonKeyValueElement.addAttribute(new Attribute("test", "criterion.additionalCondition == 7"));
XmlElement jsonIfMysqlElement = new XmlElement("if");
jsonIfMysqlElement.addAttribute(new Attribute("test", "_databaseId == 'mysql'"));
XmlElement sameIfMysqlElement = new XmlElement("if");
sameIfMysqlElement.addAttribute(new Attribute("test", "criterion.value == criterion.secondValue"));
sameIfMysqlElement.addElement(new TextElement("and JSON_CONTAINS(${criterion.value}->>'$', #{criterion.thirdValue})"));
jsonIfMysqlElement.addElement(sameIfMysqlElement);
XmlElement notSameIfMysqlElement = new XmlElement("if");
notSameIfMysqlElement.addAttribute(new Attribute("test", "criterion.value != criterion.secondValue"));
notSameIfMysqlElement.addElement(new TextElement("and JSON_CONTAINS(${criterion.value}->>'$.${criterion.secondValue}', #{criterion.thirdValue})"));
jsonIfMysqlElement.addElement(notSameIfMysqlElement);
jsonKeyValueElement.addElement(jsonIfMysqlElement);
XmlElement jsonIfPostgresqlElement = new XmlElement("if");
jsonIfPostgresqlElement.addAttribute(new Attribute("test", "_databaseId == 'postgresql'"));
XmlElement sameIfPostgresqlElement = new XmlElement("if");
sameIfPostgresqlElement.addAttribute(new Attribute("test", "criterion.value == criterion.secondValue"));
sameIfPostgresqlElement.addElement(new TextElement("and ${criterion.value}::jsonb @> #{criterion.thirdValue}::jsonb"));
jsonIfPostgresqlElement.addElement(sameIfPostgresqlElement);
XmlElement notSameIfPostgresqlElement = new XmlElement("if");
notSameIfPostgresqlElement.addAttribute(new Attribute("test", "criterion.value != criterion.secondValue"));
notSameIfPostgresqlElement.addElement(new TextElement("and (${criterion.value}->>'${criterion.secondValue}')::jsonb @> #{criterion.thirdValue}::jsonb"));
jsonIfPostgresqlElement.addElement(notSameIfPostgresqlElement);
jsonKeyValueElement.addElement(jsonIfPostgresqlElement);
XmlElement jsonIfNotMysqlElement = new XmlElement("if");
jsonIfNotMysqlElement.addAttribute(new Attribute("test", "_databaseId != 'mysql' and _databaseId != 'postgresql'"));
jsonIfNotMysqlElement.addElement(new TextElement("<!--"));
jsonIfNotMysqlElement.addElement(new TextElement(" otherwise."));
jsonIfNotMysqlElement.addElement(new TextElement("-->"));
jsonKeyValueElement.addElement(jsonIfNotMysqlElement);
chooseChild.addElement(jsonKeyValueElement);
}
}
XmlElement functionLeftElement = new XmlElement("when");
functionLeftElement.addAttribute(new Attribute("test", "criterion.additionalCondition == 5"));
functionLeftElement.addElement(new TextElement("and ${criterion.condition} (${criterion.value}, #{criterion.secondValue})"));
chooseChild.addElement(functionLeftElement);
XmlElement functionRightElement = new XmlElement("when");
functionRightElement.addAttribute(new Attribute("test", "criterion.additionalCondition == 6"));
functionRightElement.addElement(new TextElement("and ${criterion.condition} (#{criterion.secondValue}, ${criterion.value})"));
chooseChild.addElement(functionRightElement);
/*XmlElement otherwiseElement = new XmlElement("otherwise");
otherwiseElement.addElement(new TextElement("<!--"));
otherwiseElement.addElement(new TextElement(" otherwise."));
otherwiseElement.addElement(new TextElement("-->"));
chooseChild.addElement(otherwiseElement);*/
}
return true;
}
private boolean containsBLOBColumn(IntrospectedTable introspectedTable) {
return !introspectedTable.getBLOBColumns().isEmpty();
}
}