|
17 | 17 | package com.itfsw.mybatis.generator.plugins; |
18 | 18 |
|
19 | 19 | import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; |
| 20 | +import com.sun.xml.internal.ws.util.StringUtils; |
| 21 | +import freemarker.template.utility.CollectionUtils; |
| 22 | +import freemarker.template.utility.StringUtil; |
20 | 23 | import org.mybatis.generator.api.IntrospectedTable; |
21 | 24 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; |
22 | 25 | import org.mybatis.generator.api.dom.java.Interface; |
|
29 | 32 | * --------------------------------------------------------------------------- |
30 | 33 | * |
31 | 34 | * --------------------------------------------------------------------------- |
| 35 | + * |
32 | 36 | * @author: hewei |
33 | 37 | * @time:2019/7/9 14:30 |
34 | 38 | * --------------------------------------------------------------------------- |
35 | 39 | */ |
36 | 40 | 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 | 41 |
|
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 | | - } |
| 42 | + /** |
| 43 | + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
| 44 | + * @param introspectedTable |
| 45 | + * @return |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public void initialized(IntrospectedTable introspectedTable) { |
| 49 | + super.initialized(introspectedTable); |
| 50 | + } |
64 | 51 |
|
65 | | - if (StringUtility.isTrue(properties.getProperty(key.toString()))) { |
66 | | - this.annotations.add(annotation); |
67 | | - } |
68 | | - } |
| 52 | + /** |
| 53 | + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html |
| 54 | + * @param interfaze |
| 55 | + * @param topLevelClass |
| 56 | + * @param introspectedTable |
| 57 | + * @return |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, |
| 61 | + IntrospectedTable introspectedTable) { |
69 | 62 |
|
70 | | - if (!findMapper) { |
71 | | - this.annotations.add(0, "@Mapper"); |
72 | | - } |
73 | | - } |
| 63 | + Properties properties = getProperties(); |
| 64 | + String annotationName; |
| 65 | + String annotationImport; |
| 66 | + for (Map.Entry<Object, Object> prop : properties.entrySet()) { |
| 67 | + annotationName = (String) prop.getKey(); |
| 68 | + annotationImport = (String) prop.getValue(); |
| 69 | + interfaze.addImportedType(new FullyQualifiedJavaType(annotationImport)); |
| 70 | + interfaze.addAnnotation(annotationName); |
| 71 | + } |
74 | 72 |
|
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 | | - } |
| 73 | + return true; |
| 74 | + } |
96 | 75 |
|
97 | | - return true; |
98 | | - } |
99 | 76 | } |
0 commit comments