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;
【后端|mybatis-generator使用lombok插件生成getter、setter】具体内容
@Slf4j
public class LombokPlugin extends PluginAdapter {private final Collection annotations;
public LombokPlugin() {
annotations = new LinkedHashSet<>(Annotations.values().length);
}/**
* @param warnings list of warnings
* @return always true
*/
public boolean validate(List> 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 methodthe getter, or accessor, method generated for the specified
*column
* @param topLevelClassthe partially implemented model class
* @param introspectedColumn The class containing information about the column related
*to this field as introspected from the database
* @param introspectedTableThe class containing information about the table as
*introspected from the database
* @param modelClassTypethe 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 methodthe setter, or mutator, method generated for the specified
*column
* @param topLevelClassthe partially implemented model class
* @param introspectedColumn The class containing information about the column related
*to this field as introspected from the database
* @param introspectedTableThe class containing information about the table as
*introspected from the database
* @param modelClassTypethe 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 = https://www.it610.com/article/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 = https://www.it610.com/article/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> options;
Annotations(String paramName, String name, String className) {
this.paramName = paramName;
this.name = name;
this.javaType = new FullyQualifiedJavaType(className);
this.options = new ArrayList>();
}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 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 +
'}';
}
}
}
推荐阅读
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- c语言|C语言初期学习遇到的特殊点 【三子棋详解】【初学者福音,详细总结,复习能手】
- Python|Python实战(使用线性回归预测房价)
- IC|数字IC后端真的不如前端设计和验证吗()
- Python|教你写个简单好用的Python脚本一键自动整理文件非常适合办公用~
- python|oeasy教您玩转python - 007 - # 字符本质
- vue.js|后端开发学习Vue(一)
- Go|Docker后端部署详解(Go+Nginx)
- 后台|NATAPP内网穿透通过nginx实现一个端口访问多个不同端口服务