Skip to content

Commit 095470f

Browse files
author
hewei
committed
逻辑删除插件增加基于注解的枚举实现
1 parent 236b388 commit 095470f

File tree

5 files changed

+388
-181
lines changed

5 files changed

+388
-181
lines changed

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

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,44 +77,17 @@ public void initialized(IntrospectedTable introspectedTable) {
7777
for (String enumColumnsStr : enumColumnsStrs) {
7878
IntrospectedColumn column = IntrospectedTableTools.safeGetColumn(introspectedTable, enumColumnsStr);
7979
if (column != null) {
80-
String remarks = column.getRemarks();
81-
String javaType = column.getFullyQualifiedJavaType().getFullyQualifiedName();
82-
83-
if (!StringUtility.stringHasValue(remarks) || !remarks.matches(REMARKS_PATTERN)) {
84-
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "没有找到column为" + enumColumnsStr.trim() + "对应格式的注释的字段!");
85-
} else if (!(Short.class.getTypeName().equals(javaType)
86-
|| Integer.class.getTypeName().equals(javaType)
87-
|| Long.class.getTypeName().equals(javaType)
88-
|| Boolean.class.getTypeName().equals(javaType)
89-
|| Double.class.getTypeName().equals(javaType)
90-
|| Float.class.getTypeName().equals(javaType)
91-
|| BigDecimal.class.getTypeName().equals(javaType)
92-
|| Byte.class.getTypeName().equals(javaType)
93-
|| String.class.getTypeName().equals(javaType))) {
94-
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为" + enumColumnsStr.trim() + "对应Java类型不在支持范围内!");
95-
} else {
96-
// 提取信息
97-
Pattern pattern = Pattern.compile(NEED_PATTERN);
98-
Matcher matcher = pattern.matcher(remarks);
80+
try {
9981
EnumInfo enumInfo = new EnumInfo(column);
100-
if (matcher.find() && matcher.groupCount() == 2) {
101-
String enumInfoStr = matcher.group(1);
102-
// 根据逗号切分
103-
String[] enumInfoStrs = enumInfoStr.split(",");
104-
105-
// 提取每个节点信息
106-
for (String enumInfoItemStr : enumInfoStrs) {
107-
pattern = Pattern.compile(ITEM_PATTERN);
108-
matcher = pattern.matcher(enumInfoItemStr.trim());
109-
if (matcher.find() && matcher.groupCount() == 3) {
110-
enumInfo.addItem(matcher.group(1), matcher.group(3), matcher.group(2));
111-
}
112-
}
113-
}
114-
82+
// 解析注释
83+
enumInfo.parseRemarks(column.getRemarks());
11584
if (enumInfo.hasItems()) {
11685
this.enumColumns.put(column.getJavaProperty(), enumInfo);
11786
}
87+
} catch (EnumInfo.CannotParseException e) {
88+
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "没有找到column为" + enumColumnsStr.trim() + "对应格式的注释的字段!");
89+
} catch (EnumInfo.NotSupportTypeException e) {
90+
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为" + enumColumnsStr.trim() + "对应Java类型不在支持范围内!");
11891
}
11992
}
12093
}
@@ -166,15 +139,29 @@ public static class EnumInfo {
166139
private List<EnumItemInfo> items = new ArrayList<>();
167140
private IntrospectedColumn column;
168141

169-
public EnumInfo(IntrospectedColumn column) {
170-
this.column = column;
142+
public EnumInfo(IntrospectedColumn column) throws NotSupportTypeException, CannotParseException {
143+
String javaType = column.getFullyQualifiedJavaType().getFullyQualifiedName();
144+
if (!(Short.class.getTypeName().equals(javaType)
145+
|| Integer.class.getTypeName().equals(javaType)
146+
|| Long.class.getTypeName().equals(javaType)
147+
|| Boolean.class.getTypeName().equals(javaType)
148+
|| Double.class.getTypeName().equals(javaType)
149+
|| Float.class.getTypeName().equals(javaType)
150+
|| BigDecimal.class.getTypeName().equals(javaType)
151+
|| Byte.class.getTypeName().equals(javaType)
152+
|| String.class.getTypeName().equals(javaType))) {
153+
throw new NotSupportTypeException();
154+
} else {
155+
this.column = column;
156+
}
171157
}
172158

173159
/**
174160
* 添加Enum Item
175161
* @param name
176162
* @param comment
177163
* @param value
164+
* @return
178165
*/
179166
public void addItem(String name, String comment, String value) {
180167
items.add(new EnumItemInfo(this.column, name, comment, value));
@@ -188,6 +175,49 @@ public boolean hasItems() {
188175
return items.size() > 0;
189176
}
190177

178+
/**
179+
* 解析注释
180+
* @param remarks
181+
*/
182+
public void parseRemarks(String remarks) throws CannotParseException {
183+
if (!StringUtility.stringHasValue(remarks) || !remarks.matches(REMARKS_PATTERN)) {
184+
throw new CannotParseException();
185+
} else {
186+
// 提取信息
187+
Pattern pattern = Pattern.compile(NEED_PATTERN);
188+
Matcher matcher = pattern.matcher(remarks);
189+
if (matcher.find() && matcher.groupCount() == 2) {
190+
String enumInfoStr = matcher.group(1);
191+
// 根据逗号切分
192+
String[] enumInfoStrs = enumInfoStr.split(",");
193+
194+
// 提取每个节点信息
195+
for (String enumInfoItemStr : enumInfoStrs) {
196+
pattern = Pattern.compile(ITEM_PATTERN);
197+
matcher = pattern.matcher(enumInfoItemStr.trim());
198+
if (matcher.find() && matcher.groupCount() == 3) {
199+
this.addItem(matcher.group(1), matcher.group(3), matcher.group(2));
200+
}
201+
}
202+
}
203+
}
204+
}
205+
206+
/**
207+
* Getter method for property <tt>items</tt>.
208+
* @return property value of items
209+
* @author hewei
210+
*/
211+
public List<EnumItemInfo> getItems() {
212+
return items;
213+
}
214+
215+
public class NotSupportTypeException extends Exception {
216+
}
217+
218+
public class CannotParseException extends Exception {
219+
}
220+
191221
public InnerEnum generateEnum(CommentGenerator commentGenerator, IntrospectedTable introspectedTable) {
192222
String enumName = FormatTools.upFirstChar(column.getJavaProperty());
193223

@@ -242,7 +272,7 @@ public InnerEnum generateEnum(CommentGenerator commentGenerator, IntrospectedTab
242272
}
243273

244274

245-
private class EnumItemInfo {
275+
public class EnumItemInfo {
246276
private IntrospectedColumn column;
247277
private String name;
248278
private String comment;
@@ -286,6 +316,15 @@ public String getValue() {
286316
return "new " + javaType + "(\"" + value + "\")";
287317
}
288318
}
319+
320+
/**
321+
* Getter method for property <tt>value</tt>.
322+
* @return property value of value
323+
* @author hewei
324+
*/
325+
public String getOriginalValue() {
326+
return value;
327+
}
289328
}
290329
}
291330
}

0 commit comments

Comments
 (0)