package info.qiuranke.mybatis.generator.plugins;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
public class MapperPlugin extends PluginAdapter {
private final Collection<Annotations> annotations;
public MapperPlugin() {
annotations = new HashSet<Annotations>(Annotations.values().length);
}
public boolean validate(List<String> warnings) {
return true;
}
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
super.clientGenerated(interfaze, topLevelClass, introspectedTable);
addDataAnnotation(interfaze);
return true;
}
private void addDataAnnotation(Interface interfaze) {
for (Annotations annotation : annotations) {
interfaze.addImportedType(annotation.javaType);
interfaze.addAnnotation(annotation.name);
}
}
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
for (Entry<Object, Object> entry : properties.entrySet()) {
boolean isEnable = Boolean.parseBoolean(entry.getValue().toString());
if (isEnable) {
String paramName = entry.getKey().toString().trim();
Annotations annotation = Annotations.getValueOf(paramName);
if (annotation != null) {
annotations.add(annotation);
}
}
}
}
private enum Annotations {
DATA("mapper", "@Mapper", "org.apache.ibatis.annotations.Mapper");
private final String paramName;
private final String name;
private final FullyQualifiedJavaType javaType;
Annotations(String paramName, String name, String className) {
this.paramName = paramName;
this.name = name;
this.javaType = new FullyQualifiedJavaType(className);
}
private static Annotations getValueOf(String paramName) {
for (Annotations annotation : Annotations.values())
if (String.CASE_INSENSITIVE_ORDER.compare(paramName, annotation.paramName) == 0)
return annotation;
return null;
}
}
}
转载于:https://my.oschina.net/IBM/blog/843931