Skip to content

Commit 96da83a

Browse files
author
hewei
committed
Merge remote-tracking branch 'origin/master'
2 parents b1a287a + af3394b commit 96da83a

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

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

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,53 @@
2121
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
2222
import org.mybatis.generator.api.dom.java.Interface;
2323
import org.mybatis.generator.api.dom.java.TopLevelClass;
24-
import org.mybatis.generator.internal.util.StringUtility;
2524

26-
import java.util.*;
25+
import java.util.Map;
26+
import java.util.Properties;
2727

2828
/**
2929
* ---------------------------------------------------------------------------
3030
*
3131
* ---------------------------------------------------------------------------
32+
*
3233
* @author: hewei
3334
* @time:2019/7/9 14:30
3435
* ---------------------------------------------------------------------------
3536
*/
3637
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);
5538

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+
}
6448

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) {
6959

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+
}
7469

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+
}
9672

97-
return true;
98-
}
9973
}

0 commit comments

Comments
 (0)