Skip to content

Commit 436fbdb

Browse files
author
hewei
committed
fix#53:Example支持when
1 parent 2d3f7c3 commit 436fbdb

File tree

7 files changed

+339
-29
lines changed

7 files changed

+339
-29
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.mybatis.generator.api.IntrospectedTable;
1111
import org.mybatis.generator.api.dom.java.*;
1212
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
13+
import org.mybatis.generator.internal.util.StringUtility;
1314

1415
import java.util.List;
1516

@@ -22,9 +23,14 @@
2223
* ---------------------------------------------------------------------------
2324
*/
2425
public class ExampleEnhancedPlugin extends BasePlugin {
25-
public static final String METHOD_NEW_AND_CREATE_CRITERIA = "newAndCreateCriteria"; // newAndCreateCriteria 方法
26-
27-
private boolean enableColumnOperate = false; // 是否启用column的操作
26+
// newAndCreateCriteria 方法
27+
public static final String METHOD_NEW_AND_CREATE_CRITERIA = "newAndCreateCriteria";
28+
// 逻辑删除列-Key
29+
public static final String PRO_ENABLE_AND_IF = "enableAndIf";
30+
// 是否启用column的操作
31+
private boolean enableColumnOperate = false;
32+
// 是否启用了过期的andIf
33+
private boolean enableAndIf;
2834

2935
/**
3036
* {@inheritDoc}
@@ -34,6 +40,8 @@ public class ExampleEnhancedPlugin extends BasePlugin {
3440
public void initialized(IntrospectedTable introspectedTable) {
3541
super.initialized(introspectedTable);
3642
this.enableColumnOperate = PluginTools.checkDependencyPlugin(context, ModelColumnPlugin.class);
43+
String enableAndIf = properties.getProperty(PRO_ENABLE_AND_IF);
44+
this.enableAndIf = enableAndIf == null ? true : StringUtility.isTrue(enableAndIf);
3745
}
3846

3947
/**
@@ -46,12 +54,17 @@ public void initialized(IntrospectedTable introspectedTable) {
4654
@Override
4755
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
4856
List<InnerClass> innerClasses = topLevelClass.getInnerClasses();
49-
for (InnerClass innerClass : innerClasses) {
57+
for (int i = 0; i < innerClasses.size(); i++) {
58+
InnerClass innerClass = innerClasses.get(i);
5059
if ("Criteria".equals(innerClass.getType().getShortName())) {
5160
// 工厂方法
5261
addFactoryMethodToCriteria(topLevelClass, innerClass, introspectedTable);
5362
// andIf
54-
addAndIfMethodToCriteria(topLevelClass, innerClass, introspectedTable);
63+
if (this.enableAndIf) {
64+
addAndIfMethodToCriteria(topLevelClass, innerClass, introspectedTable);
65+
}
66+
// when
67+
addWhenToCriteria(topLevelClass, innerClass, introspectedTable);
5568
} else if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) {
5669
// column 方法
5770
if (this.enableColumnOperate) {
@@ -62,10 +75,12 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
6275

6376
List<Method> methods = topLevelClass.getMethods();
6477
for (Method method : methods) {
65-
if (!"createCriteriaInternal".equals(method.getName()))
78+
if (!"createCriteriaInternal".equals(method.getName())) {
6679
continue;
67-
method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);");
68-
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "修改createCriteriaInternal方法,修改构造Criteria时传入Example对象");
80+
} else {
81+
method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);");
82+
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "修改createCriteriaInternal方法,修改构造Criteria时传入Example对象");
83+
}
6984
}
7085

7186
// orderBy方法
@@ -74,6 +89,9 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
7489
// createCriteria 静态方法
7590
this.addStaticCreateCriteriaMethodToExample(topLevelClass, introspectedTable);
7691

92+
// when
93+
addWhenToExample(topLevelClass, introspectedTable);
94+
7795
return true;
7896
}
7997

@@ -215,17 +233,101 @@ private void addFactoryMethodToCriteria(TopLevelClass topLevelClass, InnerClass
215233
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加工厂方法example");
216234
}
217235

236+
/**
237+
* 增强Criteria的链式调用(when)
238+
* @param topLevelClass
239+
* @param innerClass
240+
* @param introspectedTable
241+
*/
242+
private void addWhenToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) {
243+
this.addWhenToClass(topLevelClass, innerClass, introspectedTable, "criteria");
244+
}
245+
246+
/**
247+
* 增强Example的链式调用(when)
248+
* @param topLevelClass
249+
* @param introspectedTable
250+
*/
251+
private void addWhenToExample(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
252+
this.addWhenToClass(topLevelClass, topLevelClass, introspectedTable, "example");
253+
}
254+
255+
/**
256+
* 增强链式调用(when)
257+
* @param topLevelClass
258+
* @param clazz
259+
* @param introspectedTable
260+
*/
261+
private void addWhenToClass(TopLevelClass topLevelClass, InnerClass clazz, IntrospectedTable introspectedTable, String type) {
262+
// 添加接口When
263+
InnerInterface whenInterface = new InnerInterface("I" + FormatTools.upFirstChar(type) + "When");
264+
whenInterface.setVisibility(JavaVisibility.PUBLIC);
265+
266+
// ICriteriaAdd增加接口add
267+
Method addMethod = JavaElementGeneratorTools.generateMethod(
268+
type,
269+
JavaVisibility.DEFAULT,
270+
null,
271+
new Parameter(clazz.getType(), type)
272+
);
273+
commentGenerator.addGeneralMethodComment(addMethod, introspectedTable);
274+
whenInterface.addMethod(addMethod);
275+
276+
InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass(whenInterface);
277+
// 添加注释
278+
commentGenerator.addClassComment(innerClassWrapper, introspectedTable);
279+
topLevelClass.addInnerClass(innerClassWrapper);
280+
281+
// 添加when方法
282+
Method whenMethod = JavaElementGeneratorTools.generateMethod(
283+
"when",
284+
JavaVisibility.PUBLIC,
285+
clazz.getType(),
286+
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "condition"),
287+
new Parameter(whenInterface.getType(), "then")
288+
);
289+
commentGenerator.addGeneralMethodComment(whenMethod, introspectedTable);
290+
whenMethod = JavaElementGeneratorTools.generateMethodBody(
291+
whenMethod,
292+
"if (condition) {",
293+
"then." + type + "(this);",
294+
"}",
295+
"return this;"
296+
);
297+
FormatTools.addMethodWithBestPosition(clazz, whenMethod);
298+
Method whenOtherwiseMethod = JavaElementGeneratorTools.generateMethod(
299+
"when",
300+
JavaVisibility.PUBLIC,
301+
clazz.getType(),
302+
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "condition"),
303+
new Parameter(whenInterface.getType(), "then"),
304+
new Parameter(whenInterface.getType(), "otherwise")
305+
);
306+
commentGenerator.addGeneralMethodComment(whenOtherwiseMethod, introspectedTable);
307+
whenOtherwiseMethod = JavaElementGeneratorTools.generateMethodBody(
308+
whenOtherwiseMethod,
309+
"if (condition) {",
310+
"then." + type + "(this);",
311+
"} else {",
312+
"otherwise." + type + "(this);",
313+
"}",
314+
"return this;"
315+
);
316+
FormatTools.addMethodWithBestPosition(clazz, whenOtherwiseMethod);
317+
}
218318

219319
/**
220320
* 增强Criteria的链式调用,添加andIf(boolean addIf, CriteriaAdd add)方法,实现链式调用中按条件增加查询语句
221321
* @param topLevelClass
222322
* @param innerClass
223323
* @param introspectedTable
224324
*/
325+
@Deprecated
225326
private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) {
226327
// 添加接口CriteriaAdd
227328
InnerInterface criteriaAddInterface = new InnerInterface("ICriteriaAdd");
228329
criteriaAddInterface.setVisibility(JavaVisibility.PUBLIC);
330+
criteriaAddInterface.addAnnotation("@Deprecated");
229331
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "." + innerClass.getType().getShortName() + "增加接口ICriteriaAdd");
230332

231333
// ICriteriaAdd增加接口add
@@ -252,6 +354,7 @@ private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass in
252354
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "ifAdd"),
253355
new Parameter(criteriaAddInterface.getType(), "add")
254356
);
357+
andIfMethod.addAnnotation("@Deprecated");
255358
commentGenerator.addGeneralMethodComment(andIfMethod, introspectedTable);
256359
andIfMethod = JavaElementGeneratorTools.generateMethodBody(
257360
andIfMethod,

src/main/java/com/itfsw/mybatis/generator/plugins/ExampleTargetPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
public class ExampleTargetPlugin extends BasePlugin {
3838
public static final String PRO_TARGET_PACKAGE = "targetPackage"; // 配置targetPackage名
39-
private static String targetPackage; // 目标包
39+
private String targetPackage; // 目标包
4040

4141
/**
4242
* {@inheritDoc}

src/main/java/com/itfsw/mybatis/generator/plugins/TableRenamePlugin.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.itfsw.mybatis.generator.plugins;
1818

1919
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
20+
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
2021
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
2122
import org.mybatis.generator.api.IntrospectedTable;
2223
import org.mybatis.generator.config.TableConfiguration;
@@ -88,7 +89,7 @@ public void initialized(IntrospectedTable introspectedTable) {
8889
Matcher matcher = pattern.matcher(domainObjectName);
8990
domainObjectName = matcher.replaceAll(replaceString);
9091
// 命名规范化 首字母大写
91-
domainObjectName = upFirstWord(domainObjectName);
92+
domainObjectName = FormatTools.upFirstChar(domainObjectName);
9293
try {
9394
IntrospectedTableTools.setDomainObjectName(introspectedTable, getContext(), domainObjectName);
9495
} catch (Exception e) {
@@ -97,13 +98,4 @@ public void initialized(IntrospectedTable introspectedTable) {
9798
}
9899
super.initialized(introspectedTable);
99100
}
100-
101-
/**
102-
* 字符串首字母大写
103-
* @param str
104-
* @return
105-
*/
106-
private String upFirstWord(String str) {
107-
return str.substring(0, 1).toUpperCase() + str.substring(1);
108-
}
109101
}

src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,13 @@ public static void replaceComment(CommentGenerator commentGenerator, XmlElement
235235
element.addElement(0, tmpEle.getElements().get(i));
236236
}
237237
}
238+
239+
/**
240+
* 首字母大写
241+
* @param str
242+
* @return
243+
*/
244+
public static String upFirstChar(String str) {
245+
return str.substring(0, 1).toUpperCase() + str.substring(1);
246+
}
238247
}

src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static Method generateMethodBody(Method method, String... bodyLines) {
109109
*/
110110
public static Method generateSetterMethod(Field field) {
111111
Method method = generateMethod(
112-
"set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
112+
"set" + FormatTools.upFirstChar(field.getName()),
113113
JavaVisibility.PUBLIC,
114114
null,
115115
new Parameter(field.getType(), field.getName())
@@ -124,7 +124,7 @@ public static Method generateSetterMethod(Field field) {
124124
*/
125125
public static Method generateGetterMethod(Field field) {
126126
Method method = generateMethod(
127-
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
127+
"get" + FormatTools.upFirstChar(field.getName()),
128128
JavaVisibility.PUBLIC,
129129
field.getType()
130130
);

src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerInterface.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public InnerInterface(String type) {
4949
* @param compilationUnit the compilation unit
5050
* @return the formatted content
5151
*/
52+
@Override
5253
public String getFormattedContent(int indentLevel, CompilationUnit compilationUnit) {
5354
StringBuilder sb = new StringBuilder();
5455

@@ -58,15 +59,15 @@ public String getFormattedContent(int indentLevel, CompilationUnit compilationUn
5859
}
5960

6061
if (stringHasValue(getType().getPackageName())) {
61-
sb.append("package "); //$NON-NLS-1$
62+
sb.append("package ");
6263
sb.append(getType().getPackageName());
6364
sb.append(';');
6465
newLine(sb);
6566
newLine(sb);
6667
}
6768

6869
for (String staticImport : getStaticImports()) {
69-
sb.append("import static "); //$NON-NLS-1$
70+
sb.append("import static ");
7071
sb.append(staticImport);
7172
sb.append(';');
7273
newLine(sb);
@@ -94,19 +95,19 @@ public String getFormattedContent(int indentLevel, CompilationUnit compilationUn
9495
sb.append(getVisibility().getValue());
9596

9697
if (isFinal()) {
97-
sb.append("final "); //$NON-NLS-1$
98+
sb.append("final ");
9899
}
99100

100-
sb.append("interface "); //$NON-NLS-1$
101+
sb.append("interface ");
101102
sb.append(getType().getShortName());
102103

103104
if (getSuperInterfaceTypes().size() > 0) {
104-
sb.append(" extends "); //$NON-NLS-1$
105+
sb.append(" extends ");
105106

106107
boolean comma = false;
107108
for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes()) {
108109
if (comma) {
109-
sb.append(", "); //$NON-NLS-1$
110+
sb.append(", ");
110111
} else {
111112
comma = true;
112113
}
@@ -115,7 +116,7 @@ public String getFormattedContent(int indentLevel, CompilationUnit compilationUn
115116
}
116117
}
117118

118-
sb.append(" {"); //$NON-NLS-1$
119+
sb.append(" {");
119120
indentLevel++;
120121

121122
Iterator<Method> mtdIter = getMethods().iterator();

0 commit comments

Comments
 (0)