Skip to content

Commit 22e87bf

Browse files
committed
✏️ mapper annotation 自定义,不约束死导入的内容,取决于用户自定义的注解类型及包路径
1 parent e49da96 commit 22e87bf

File tree

1 file changed

+34
-57
lines changed

1 file changed

+34
-57
lines changed

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

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.itfsw.mybatis.generator.plugins;
1818

1919
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;
2023
import org.mybatis.generator.api.IntrospectedTable;
2124
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
2225
import org.mybatis.generator.api.dom.java.Interface;
@@ -29,71 +32,45 @@
2932
* ---------------------------------------------------------------------------
3033
*
3134
* ---------------------------------------------------------------------------
35+
*
3236
* @author: hewei
3337
* @time:2019/7/9 14:30
3438
* ---------------------------------------------------------------------------
3539
*/
3640
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);
5541

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

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

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

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

97-
return true;
98-
}
9976
}

0 commit comments

Comments
 (0)