|
21 | 21 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; |
22 | 22 | import org.mybatis.generator.api.dom.java.Interface; |
23 | 23 | import org.mybatis.generator.api.dom.java.TopLevelClass; |
24 | | -import org.mybatis.generator.internal.util.StringUtility; |
25 | 24 |
|
26 | | -import java.util.*; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Properties; |
27 | 27 |
|
28 | 28 | /** |
29 | 29 | * --------------------------------------------------------------------------- |
30 | 30 | * |
31 | 31 | * --------------------------------------------------------------------------- |
| 32 | + * |
32 | 33 | * @author: hewei |
33 | 34 | * @time:2019/7/9 14:30 |
34 | 35 | * --------------------------------------------------------------------------- |
35 | 36 | */ |
36 | 37 | public class MapperAnnotationPlugin extends BasePlugin { |
37 | | - private final static Map<String, String> ANNOTATION_IMPORTS; |
38 | | - |
39 | | - static { |
40 | | - ANNOTATION_IMPORTS = new HashMap<>(); |
41 | | - ANNOTATION_IMPORTS.put("@Mapper", "org.apache.ibatis.annotations.Mapper"); |
42 | | - ANNOTATION_IMPORTS.put("@Repository", "org.springframework.stereotype.Repository"); |
43 | | - } |
44 | | - |
45 | | - private List<String> annotations; |
46 | | - |
47 | | - /** |
48 | | - * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
49 | | - * @param introspectedTable |
50 | | - * @return |
51 | | - */ |
52 | | - @Override |
53 | | - public void initialized(IntrospectedTable introspectedTable) { |
54 | | - super.initialized(introspectedTable); |
55 | 38 |
|
56 | | - this.annotations = new ArrayList<>(); |
57 | | - Properties properties = this.getProperties(); |
58 | | - boolean findMapper = false; |
59 | | - for (Object key : properties.keySet()) { |
60 | | - String annotation = key.toString().trim(); |
61 | | - if (annotation.startsWith("@Mapper")) { |
62 | | - findMapper = true; |
63 | | - } |
| 39 | + /** |
| 40 | + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
| 41 | + * @param introspectedTable |
| 42 | + * @return |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public void initialized(IntrospectedTable introspectedTable) { |
| 46 | + super.initialized(introspectedTable); |
| 47 | + } |
64 | 48 |
|
65 | | - if (StringUtility.isTrue(properties.getProperty(key.toString()))) { |
66 | | - this.annotations.add(annotation); |
67 | | - } |
68 | | - } |
| 49 | + /** |
| 50 | + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
| 51 | + * @param interfaze |
| 52 | + * @param topLevelClass |
| 53 | + * @param introspectedTable |
| 54 | + * @return |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, |
| 58 | + IntrospectedTable introspectedTable) { |
69 | 59 |
|
70 | | - if (!findMapper) { |
71 | | - this.annotations.add(0, "@Mapper"); |
72 | | - } |
73 | | - } |
| 60 | + Properties properties = getProperties(); |
| 61 | + String annotationName; |
| 62 | + String annotationImport; |
| 63 | + for (Map.Entry<Object, Object> prop : properties.entrySet()) { |
| 64 | + annotationName = (String) prop.getKey(); |
| 65 | + annotationImport = (String) prop.getValue(); |
| 66 | + interfaze.addImportedType(new FullyQualifiedJavaType(annotationImport)); |
| 67 | + interfaze.addAnnotation(annotationName); |
| 68 | + } |
74 | 69 |
|
75 | | - /** |
76 | | - * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
77 | | - * @param interfaze |
78 | | - * @param topLevelClass |
79 | | - * @param introspectedTable |
80 | | - * @return |
81 | | - */ |
82 | | - @Override |
83 | | - public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { |
84 | | - for (String annotation : this.annotations) { |
85 | | - if (annotation.equals("@Mapper")) { |
86 | | - if (introspectedTable.getTargetRuntime() == IntrospectedTable.TargetRuntime.MYBATIS3) { |
87 | | - // don't need to do this for MYBATIS3_DSQL as that runtime already adds this annotation |
88 | | - interfaze.addImportedType(new FullyQualifiedJavaType(ANNOTATION_IMPORTS.get(annotation))); |
89 | | - interfaze.addAnnotation(annotation); |
90 | | - } |
91 | | - } else if (ANNOTATION_IMPORTS.get(annotation) != null) { |
92 | | - interfaze.addImportedType(new FullyQualifiedJavaType(ANNOTATION_IMPORTS.get(annotation))); |
93 | | - interfaze.addAnnotation(annotation); |
94 | | - } |
95 | | - } |
| 70 | + return true; |
| 71 | + } |
96 | 72 |
|
97 | | - return true; |
98 | | - } |
99 | 73 | } |
0 commit comments