-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelAnnotationPlugin.java
71 lines (56 loc) · 2.44 KB
/
ModelAnnotationPlugin.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
package com.revengemission.plugins.mybatis;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Entity Model 类上添加注解,可添加多个注解
*/
public class ModelAnnotationPlugin extends AbstractXmbgPlugin {
private static final String EVERY_TABLE_NAME = "every_table";
@Override
public boolean validate(List<String> warnings) {
return true;
}
private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String currentTableName = getTableName(introspectedTable);
//<table name ,<annotationClass,annotationValue>>
Map<String, Map<String, String>> todo = new LinkedHashMap<>();
properties.forEach((k, v) -> {
//截取property name,是因为类上可能有多个注解,防止key重复覆盖
String[] temp = k.toString().trim().split(";");
if (temp.length == 2) {
if (todo.containsKey(temp[0])) {
todo.get(temp[0]).put(temp[1], v.toString().trim());
} else {
Map<String, String> annotationMap = new HashMap<>(16);
annotationMap.put(temp[1], v.toString().trim());
todo.put(temp[0], annotationMap);
}
}
});
todo.forEach((k, v) -> {
if (currentTableName.equalsIgnoreCase(k) || EVERY_TABLE_NAME.equalsIgnoreCase(k)) {
v.forEach((annotationClass, annotationValue) -> {
topLevelClass.addImportedType(new FullyQualifiedJavaType(annotationClass));
topLevelClass.addAnnotation(annotationValue);
});
}
});
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
addAnnotations(topLevelClass, introspectedTable);
return true;
}
@Override
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
addAnnotations(topLevelClass, introspectedTable);
return true;
}
}