|
@@ -18,13 +18,13 @@ package com.baomidou.mybatisplus.generator.config.builder;
|
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
|
import com.baomidou.mybatisplus.annotation.TableField;
|
|
|
import com.baomidou.mybatisplus.annotation.TableId;
|
|
|
-import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.generator.DefaultTableAnnotationHandler;
|
|
|
import com.baomidou.mybatisplus.generator.DefaultTableFieldAnnotationHandler;
|
|
|
import com.baomidou.mybatisplus.generator.IFill;
|
|
|
+import com.baomidou.mybatisplus.generator.ITableAnnotationHandler;
|
|
|
import com.baomidou.mybatisplus.generator.ITableFieldAnnotationHandler;
|
|
|
import com.baomidou.mybatisplus.generator.ITemplate;
|
|
|
import com.baomidou.mybatisplus.generator.config.ConstVal;
|
|
@@ -77,7 +77,7 @@ public class Entity implements ITemplate {
|
|
|
private String javaTemplate = ConstVal.TEMPLATE_ENTITY_JAVA;
|
|
|
|
|
|
/**
|
|
|
- * Kotlin模板默认撸
|
|
|
+ * Kotlin模板默认路径
|
|
|
*/
|
|
|
@Getter
|
|
|
private String kotlinTemplate = ConstVal.TEMPLATE_ENTITY_KT;
|
|
@@ -233,10 +233,34 @@ public class Entity implements ITemplate {
|
|
|
private boolean generate = true;
|
|
|
|
|
|
/**
|
|
|
- * 默认lombok (兼容属性只有Getter和Setter)
|
|
|
+ * 默认lombok(低版本属性默认只有Getter和Setter)
|
|
|
+ * <p>当升级至3.5.10后,默认启用@ToString,如果不需要,可通过{@link Builder#toString(boolean)}关闭</p>
|
|
|
+ *
|
|
|
+ * @since 3.5.10
|
|
|
*/
|
|
|
+ @Getter
|
|
|
private boolean defaultLombok = true;
|
|
|
|
|
|
+ /**
|
|
|
+ * 是否生成ToString
|
|
|
+ * <p>低版本下,lombok没有处理ToString逻辑,现在处理生成@ToString</p>
|
|
|
+ * <p>支持控制toString方法是否生成</p>
|
|
|
+ *
|
|
|
+ * @since 3.5.10
|
|
|
+ */
|
|
|
+ @Getter
|
|
|
+ private boolean toString = true;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启用字段文档注释 (当注释字段注释不为空才生效)
|
|
|
+ * <p>低版本下,如果是启用swagger或者springdoc时,不会生成,现在统一修改为生成文档注释</p>
|
|
|
+ *
|
|
|
+ * @since 3.5.10
|
|
|
+ */
|
|
|
+ @Getter
|
|
|
+ private boolean fieldUseJavaDoc = true;
|
|
|
+
|
|
|
/**
|
|
|
* 实体类注解
|
|
|
*
|
|
@@ -245,6 +269,13 @@ public class Entity implements ITemplate {
|
|
|
@Getter
|
|
|
private final List<ClassAnnotationAttributes> classAnnotations = new ArrayList<>();
|
|
|
|
|
|
+ /**
|
|
|
+ * 表注解处理器
|
|
|
+ *
|
|
|
+ * @since 3.5.10
|
|
|
+ */
|
|
|
+ private ITableAnnotationHandler tableAnnotationHandler = new DefaultTableAnnotationHandler();
|
|
|
+
|
|
|
/**
|
|
|
* 字段注解处理器
|
|
|
*
|
|
@@ -371,46 +402,18 @@ public class Entity implements ITemplate {
|
|
|
data.put("entityLombokModel", this.lombok);
|
|
|
data.put("entityBooleanColumnRemoveIsPrefix", this.booleanColumnRemoveIsPrefix);
|
|
|
data.put("superEntityClass", ClassUtils.getSimpleName(this.superClass));
|
|
|
- GlobalConfig globalConfig = tableInfo.getGlobalConfig();
|
|
|
- String comment = tableInfo.getComment();
|
|
|
Set<String> importPackages = new HashSet<>(tableInfo.getImportPackages());
|
|
|
- if (StringUtils.isBlank(comment)) {
|
|
|
- comment = StringPool.EMPTY;
|
|
|
- }
|
|
|
- boolean kotlin = globalConfig.isKotlin();
|
|
|
- if (!kotlin) {
|
|
|
- // 原先kt模板没有处理这些,作为兼容项
|
|
|
- if (chain) {
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
|
|
|
- }
|
|
|
- if (lombok && defaultLombok) {
|
|
|
- // 原先lombok默认只有这两个
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
|
|
|
+ List<ClassAnnotationAttributes> classAnnotationAttributes = new ArrayList<>(this.getClassAnnotations());
|
|
|
+ if (tableAnnotationHandler != null) {
|
|
|
+ List<ClassAnnotationAttributes> classAnnotationAttributesList = tableAnnotationHandler.handle(tableInfo, this);
|
|
|
+ if (classAnnotationAttributesList != null && !classAnnotationAttributesList.isEmpty()) {
|
|
|
+ classAnnotationAttributes.addAll(classAnnotationAttributesList);
|
|
|
}
|
|
|
}
|
|
|
- if (tableInfo.isConvert()) {
|
|
|
- String schemaName = tableInfo.getSchemaName();
|
|
|
- if (StringUtils.isBlank(schemaName)) {
|
|
|
- schemaName = StringPool.EMPTY;
|
|
|
- } else {
|
|
|
- schemaName = schemaName + StringPool.DOT;
|
|
|
- }
|
|
|
- //@TableName("${schemaName}${table.name}")
|
|
|
- String displayName = String.format("@TableName(\"%s%s\")", schemaName, tableInfo.getName());
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes(TableName.class, displayName));
|
|
|
- }
|
|
|
- if (globalConfig.isSwagger()) {
|
|
|
- //@ApiModel(value = "${entity}对象", description = "${table.comment!}")
|
|
|
- String displayName = String.format("@ApiModel(value = \"%s对象\", description = \"%s\")", tableInfo.getEntityName(), comment);
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes(
|
|
|
- displayName, "io.swagger.annotations.ApiModel", "io.swagger.annotations.ApiModelProperty"));
|
|
|
- }
|
|
|
- if (globalConfig.isSpringdoc()) {
|
|
|
- //@Schema(name = "${entity}", description = "${table.comment!}")
|
|
|
- String displayName = String.format("@Schema(name = \"%s\", description = \"%s\")", tableInfo.getEntityName(), comment);
|
|
|
- this.classAnnotations.add(new ClassAnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
|
|
|
- }
|
|
|
+ classAnnotationAttributes.forEach(attributes -> {
|
|
|
+ attributes.handleDisplayName(tableInfo);
|
|
|
+ importPackages.addAll(attributes.getImportPackages());
|
|
|
+ });
|
|
|
if (tableFieldAnnotationHandler != null) {
|
|
|
tableInfo.getFields().forEach(tableField -> {
|
|
|
List<AnnotationAttributes> annotationAttributes = tableFieldAnnotationHandler.handle(tableInfo, tableField);
|
|
@@ -420,15 +423,11 @@ public class Entity implements ITemplate {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- this.classAnnotations.forEach(attributes -> {
|
|
|
- attributes.handleDisplayName(tableInfo);
|
|
|
- importPackages.addAll(attributes.getImportPackages());
|
|
|
- });
|
|
|
- //TODO 外部暂时不要使用此属性,内置模板暂时使用
|
|
|
- data.put("useJavaDoc", !(globalConfig.isSwagger() || globalConfig.isSpringdoc()));
|
|
|
- data.put("entityClassAnnotations", this.classAnnotations.stream()
|
|
|
+ data.put("entityFieldUseJavaDoc", fieldUseJavaDoc);
|
|
|
+ data.put("entityClassAnnotations", classAnnotationAttributes.stream()
|
|
|
.sorted(Comparator.comparingInt(s -> s.getDisplayName().length())).collect(Collectors.toList()));
|
|
|
data.put("importEntityPackages", importPackages.stream().sorted().collect(Collectors.toList()));
|
|
|
+ data.put("entityToString", this.toString);
|
|
|
return data;
|
|
|
}
|
|
|
|
|
@@ -508,6 +507,7 @@ public class Entity implements ITemplate {
|
|
|
|
|
|
/**
|
|
|
* 开启lombok模型 (默认添加Getter和Setter)
|
|
|
+ * <p>自3.5.10开始,默认添加ToString搭配,如果想关闭可通过{@link #toString(boolean)}关闭</p>
|
|
|
*
|
|
|
* @return this
|
|
|
* @since 3.5.0
|
|
@@ -518,7 +518,10 @@ public class Entity implements ITemplate {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 开启lombok模型 (这里会把注解属性都加入进去,无论是否启用{@link GlobalConfig#isKotlin()})
|
|
|
+ * 开启lombok模型 (会把注解属性都加入进去,无论是否启用{@link GlobalConfig#isKotlin()})
|
|
|
+ * <p>注意如果通过此方法开启lombok模型,默认的lombok注解(get,set,toString)都将不会生成,请自行控制添加</p>
|
|
|
+ * <p>由{@link #toString(boolean)}控制的也会失效</p>
|
|
|
+ * 使用@Data示例: enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))
|
|
|
*
|
|
|
* @param attributes 注解属性集合
|
|
|
* @return this
|
|
@@ -803,6 +806,42 @@ public class Entity implements ITemplate {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 指定表注解处理器
|
|
|
+ * @param tableAnnotationHandler 表注解处理器
|
|
|
+ * @since 3.5.10
|
|
|
+ * @return this
|
|
|
+ */
|
|
|
+ public Builder tableAnnotationHandler(@NotNull ITableAnnotationHandler tableAnnotationHandler){
|
|
|
+ this.entity.tableAnnotationHandler = tableAnnotationHandler;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置是否生成ToString方法
|
|
|
+ *
|
|
|
+ * @param toString 是否生成
|
|
|
+ * @return this
|
|
|
+ * @since 3.5.10
|
|
|
+ */
|
|
|
+ public Builder toString(boolean toString) {
|
|
|
+ this.entity.toString = toString;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置字段是否生成文档注释
|
|
|
+ *
|
|
|
+ * @param fieldUseJavaDoc 是否生成文档注释
|
|
|
+ * @return this
|
|
|
+ * @since 3.5.10
|
|
|
+ */
|
|
|
+ public Builder fieldUseJavaDoc(boolean fieldUseJavaDoc) {
|
|
|
+ this.entity.fieldUseJavaDoc = fieldUseJavaDoc;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public Entity get() {
|
|
|
String superClass = this.entity.superClass;
|
|
|
if (StringUtils.isNotBlank(superClass)) {
|