import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.IntrospectedColumn;
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.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
具体内容
@Slf4j
public class LombokPlugin extends PluginAdapter {
private final Collection<Annotations> annotations;
public LombokPlugin() {
annotations = new LinkedHashSet<>(Annotations.values().length);
}
/**
* @param warnings list of warnings
* @return always true
*/
public boolean validate(List<String> warnings) {
return true;
}
/**
* 生成没有主键的model类
*
* @param topLevelClass 将要被生成的类
* @param introspectedTable 从数据库中获取到的一些信息
* @return always true
*/
@Override
public boolean modelBaseRecordClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass);
return true;
}
/**
* 生成带主键的model类
*
* @param topLevelClass 将要被生成的类
* @param introspectedTable 从数据库中获取到的一些信息
* @return always true
*/
@Override
public boolean modelPrimaryKeyClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass);
return true;
}
/**
* 生成带Blob字段的model类
*
* @param topLevelClass 将要被生成的类
* @param introspectedTable 从数据库中获取到的一些信息
* @return always true
*/
@Override
public boolean modelRecordWithBLOBsClassGenerated(
TopLevelClass topLevelClass,
IntrospectedTable introspectedTable
) {
addAnnotations(topLevelClass);
return true;
}
/**
* Prevents all getters from being generated.
* See SimpleModelGenerator
*
* @param method the getter, or accessor, method generated for the specified
* column
* @param topLevelClass the partially implemented model class
* @param introspectedColumn The class containing information about the column related
* to this field as introspected from the database
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @param modelClassType the type of class that the field is generated for
*/
@Override
public boolean modelGetterMethodGenerated(
Method method,
TopLevelClass topLevelClass,
IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable,
ModelClassType modelClassType
) {
return false;
}
/**
* Prevents all setters from being generated
* See SimpleModelGenerator
*
* @param method the setter, or mutator, method generated for the specified
* column
* @param topLevelClass the partially implemented model class
* @param introspectedColumn The class containing information about the column related
* to this field as introspected from the database
* @param introspectedTable The class containing information about the table as
* introspected from the database
* @param modelClassType the type of class that the field is generated for
* @return always false
*/
@Override
public boolean modelSetterMethodGenerated(
Method method,
TopLevelClass topLevelClass,
IntrospectedColumn introspectedColumn,
IntrospectedTable introspectedTable,
ModelClassType modelClassType
) {
return false;
}
/**
* 给目标类添加需要的注解
*
* @param topLevelClass the partially implemented model class
*/
private void addAnnotations(TopLevelClass topLevelClass) {
if (annotations.contains(Annotations.HASLOMBOK)) {
for (Annotations annotation : annotations) {
topLevelClass.addImportedType(annotation.javaType);
topLevelClass.addAnnotation(annotation.asAnnotation());
}
}
}
/**
* 处理定义的属性
* @param properties
*/
@Override
public void setProperties(Properties properties) {
super.setProperties(properties);
log.info("参数属性{}", properties);
// //@Data is default annotation
// annotations.add(Annotations.DATA);
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String annotationName = enumeration.nextElement().toString();
log.info("注解名{}", annotationName);
if (annotationName.contains(".")) {
// Not an annotation name
continue;
}
String value = properties.getProperty(annotationName);
log.info("注解值{}", value);
if (!Boolean.parseBoolean(value)) {
// The annotation is disabled, skip it
continue;
}
Annotations annotation = Annotations.getValueOf(annotationName);
log.info("获取到的注解值{}", annotation);
if (annotation == null) {
continue;
}
String optionsPrefix = annotationName + ".";
Enumeration<?> propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()) {
String propertyName = propertyNames.nextElement().toString();
log.info("属性名{}", properties);
if (!propertyName.startsWith(optionsPrefix)) {
// A property not related to this annotation
continue;
}
String propertyValue = properties.getProperty(propertyName);
log.info("属性值{}", properties);
annotation.appendOptions(propertyName, propertyValue);
}
this.annotations.add(annotation);
this.annotations.addAll(Annotations.getDependencies(annotation));
}
log.info("需要添加的注解{}", this.annotations);
}
/**
* 给生成的Mapper类添加注解
*/
// @Override
// public boolean clientGenerated(
// Interface interfaze,
// TopLevelClass topLevelClass,
// IntrospectedTable introspectedTable
// ) {
// interfaze.addImportedType(new FullyQualifiedJavaType(
// "org.apache.ibatis.annotations.Mapper"));
// interfaze.addAnnotation("@Mapper");
// return true;
// }
private enum Annotations {
// DATA("data", "@Data", "lombok.Data"),
HASLOMBOK("hasLombok", "@Data", "lombok.Data"),
BUILDER("builder", "@Builder", "lombok.Builder"),
ALL_ARGS_CONSTRUCTOR("allArgsConstructor", "@AllArgsConstructor", "lombok.AllArgsConstructor"),
NO_ARGS_CONSTRUCTOR("noArgsConstructor", "@NoArgsConstructor", "lombok.NoArgsConstructor"),
ACCESSORS("accessors", "@Accessors", "lombok.experimental.Accessors"),
TO_STRING("toString", "@ToString", "lombok.ToString");
private final String paramName;
private final String name;
private final FullyQualifiedJavaType javaType;
private final List<String> options;
Annotations(String paramName, String name, String className) {
this.paramName = paramName;
this.name = name;
this.javaType = new FullyQualifiedJavaType(className);
this.options = new ArrayList<String>();
}
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;
}
private static Collection<Annotations> getDependencies(Annotations annotation) {
if (annotation == ALL_ARGS_CONSTRUCTOR)
return Collections.singleton(NO_ARGS_CONSTRUCTOR);
else
return Collections.emptyList();
}
// A trivial quoting.
// Because Lombok annotation options type is almost String or boolean.
private static String quote(String value) {
if (Boolean.TRUE.toString().equals(value) || Boolean.FALSE.toString().equals(value))
// case of boolean, not passed as an array.
return value;
return value.replaceAll("[\\w]+", "\"$0\"");
}
private void appendOptions(String key, String value) {
String keyPart = key.substring(key.indexOf(".") + 1);
String valuePart = value.contains(",") ? String.format("{%s}", value) : value;
this.options.add(String.format("%s=%s", keyPart, quote(valuePart)));
}
private String asAnnotation() {
if (options.isEmpty()) {
return name;
}
StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("(");
boolean first = true;
for (String option : options) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(option);
}
sb.append(")");
return sb.toString();
}
@Override
public String toString() {
return "Annotations{" +
"paramName='" + paramName + '\'' +
", name='" + name + '\'' +
", javaType=" + javaType +
", options=" + options +
'}';
}
}
}