Skip to content

Commit

Permalink
支持Entity字段注解生成处理.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jan 8, 2025
1 parent 3427163 commit 82759f3
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public AutoGenerator template(@NotNull TemplateConfig templateConfig) {
*
* @param globalConfig 全局配置
* @return this
* @see 3.5.0
* @since 3.5.0
*/
public AutoGenerator global(@NotNull GlobalConfig globalConfig) {
this.globalConfig = globalConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2011-2024, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.model.AnnotationAttributes;

import java.util.ArrayList;
import java.util.List;

/**
* 默认字段注解处理器
*
* @author nieqiurong
* @since 3.5.10
*/
public class DefaultTableFieldAnnotationHandler implements ITableFieldAnnotationHandler {

@Override
public List<AnnotationAttributes> handle(TableInfo tableInfo, TableField tableField) {
List<AnnotationAttributes> annotationAttributesList = new ArrayList<>();
GlobalConfig globalConfig = tableInfo.getGlobalConfig();
Entity entity = tableField.getEntity();
String comment = tableField.getComment();
if (StringUtils.isNotBlank(comment)) {
if (globalConfig.isSpringdoc()) {
//@Schema(description = "${field.comment}")
String displayName = String.format("@Schema(description = \"%s\")", comment);
annotationAttributesList.add(new AnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
} else if (globalConfig.isSwagger()) {
String displayName = String.format("@ApiModelProperty(\"%s\")", comment);
annotationAttributesList.add(new AnnotationAttributes(displayName, "io.swagger.annotations.ApiModelProperty"));
}
}
if (tableField.isKeyFlag()) {
IdType idType = entity.getIdType();
if (tableField.isKeyIdentityFlag()) {
//@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
String displayName = String.format("@TableId(value = \"%s\", type = IdType.AUTO)", tableField.getAnnotationColumnName());
annotationAttributesList.add(new AnnotationAttributes(displayName, TableId.class.getName()));
} else if (idType != null) {
//@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
String displayName = String.format("@TableId(value = \"%s\", type = IdType.%s)", tableField.getAnnotationColumnName(), idType);
annotationAttributesList.add(new AnnotationAttributes(displayName, TableId.class.getName()));
} else if (tableField.isConvert()) {
//@TableId("${field.annotationColumnName}")
String displayName = String.format("@TableId(\"%s\")", tableField.getAnnotationColumnName());
annotationAttributesList.add(new AnnotationAttributes(displayName, TableId.class.getName()));
}
} else {
String fill = tableField.getFill();
if (StringUtils.isNotBlank(fill)) {
if (tableField.isConvert()) {
//@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
String displayName = String.format("@TableField(value = \"%s\", fill = FieldFill.%s)", tableField.getAnnotationColumnName(), fill);
annotationAttributesList.add(new AnnotationAttributes(displayName, com.baomidou.mybatisplus.annotation.TableField.class.getName()));
} else {
//@TableField(fill = FieldFill.${field.fill})
String displayName = String.format("@TableField(fill = FieldFill.%s)", fill);
annotationAttributesList.add(new AnnotationAttributes(displayName, com.baomidou.mybatisplus.annotation.TableField.class.getName()));
}
} else {
if (tableField.isConvert()) {
//@TableField("${field.annotationColumnName}")
String displayName = String.format("@TableField(\"%s\")", tableField.getAnnotationColumnName());
annotationAttributesList.add(new AnnotationAttributes(displayName, com.baomidou.mybatisplus.annotation.TableField.class.getName()));
}
}
if (tableField.isVersionField()) {
// @Version
annotationAttributesList.add(new AnnotationAttributes(Version.class));
}
if (tableField.isLogicDeleteField()) {
//@TableLogic
annotationAttributesList.add(new AnnotationAttributes(TableLogic.class));
}
}
return annotationAttributesList;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2011-2024, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.mybatisplus.generator;

import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.model.AnnotationAttributes;

import java.util.List;

/**
* 字段注解处理器
*
* @author nieqiurong
* @since 3.5.10
*/
public interface ITableFieldAnnotationHandler {

/**
* 处理字段级注解
*
* @param tableInfo 表信息
* @param tableField 字段信息
* @return 注解信息
*/
List<AnnotationAttributes> handle(TableInfo tableInfo, TableField tableField);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
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.DefaultTableFieldAnnotationHandler;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.ITableFieldAnnotationHandler;
import com.baomidou.mybatisplus.generator.ITemplate;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
Expand All @@ -32,6 +34,7 @@
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import com.baomidou.mybatisplus.generator.model.AnnotationAttributes;
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
import com.baomidou.mybatisplus.generator.util.ClassUtils;
import lombok.Getter;
Expand Down Expand Up @@ -242,6 +245,13 @@ private Entity() {
@Getter
private final List<ClassAnnotationAttributes> classAnnotations = new ArrayList<>();

/**
* 字段注解处理器
*
* @since 3.5.10
*/
private ITableFieldAnnotationHandler tableFieldAnnotationHandler = new DefaultTableFieldAnnotationHandler();

/**
* <p>
* 父类 Class 反射属性转换为公共字段
Expand Down Expand Up @@ -401,10 +411,21 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
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"));
}
if (tableFieldAnnotationHandler != null) {
tableInfo.getFields().forEach(tableField -> {
List<AnnotationAttributes> annotationAttributes = tableFieldAnnotationHandler.handle(tableInfo, tableField);
if (annotationAttributes != null && !annotationAttributes.isEmpty()) {
tableField.addAnnotationAttributesList(annotationAttributes);
annotationAttributes.forEach(attributes -> importPackages.addAll(attributes.getImportPackages()));
}
});
}
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()
.sorted(Comparator.comparingInt(s -> s.getDisplayName().length())).collect(Collectors.toList()));
data.put("importEntityPackages", importPackages.stream().sorted().collect(Collectors.toList()));
Expand Down Expand Up @@ -770,6 +791,18 @@ public Builder addClassAnnotation(@NotNull ClassAnnotationAttributes attributes)
return this;
}

/**
* 指定字段注解处理器
*
* @param tableFieldAnnotationHandler 字段处理器
* @return this
* @since 3.5.10
*/
public Builder tableFieldAnnotationHandler(@NotNull ITableFieldAnnotationHandler tableFieldAnnotationHandler) {
this.entity.tableFieldAnnotationHandler = tableFieldAnnotationHandler;
return this;
}

public Entity get() {
String superClass = this.entity.superClass;
if (StringUtils.isNotBlank(superClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.jdbc.DatabaseMetaDataWrapper;
import com.baomidou.mybatisplus.generator.model.AnnotationAttributes;
import org.apache.ibatis.type.JdbcType;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 表字段信息
Expand Down Expand Up @@ -127,6 +132,13 @@ public class TableField {
*/
private final GlobalConfig globalConfig;

/**
* 字段注解
*
* @since 3.5.10
*/
private final List<AnnotationAttributes> annotationAttributesList = new ArrayList<>();

/**
* 构造方法
*
Expand Down Expand Up @@ -351,6 +363,48 @@ public void setMetaInfo(MetaInfo metaInfo) {
this.metaInfo = metaInfo;
}

/**
* 获取实体配置信息
*
* @return 实体配置信息
* @since 3.5.10
*/
public Entity getEntity() {
return this.entity;
}

/**
* 添加字段注解属性
*
* @param annotationAttributesList 注解属性集合
* @since 3.5.10
*/
public void addAnnotationAttributesList(@NotNull List<AnnotationAttributes> annotationAttributesList) {
this.annotationAttributesList.addAll(annotationAttributesList);
}

/**
* 添加字段注解属性
*
* @param annotationAttributes 注解属性
* @since 3.5.10
*/
public void addAnnotationAttributesList(@NotNull AnnotationAttributes annotationAttributes) {
this.annotationAttributesList.add(annotationAttributes);
}

/**
* 获取字段注解属性(按{@link AnnotationAttributes#getDisplayName()}长度进行升序)
*
* @return 字段注解属性
* @since 3.5.10
*/
public List<AnnotationAttributes> getAnnotationAttributesList() {
return this.annotationAttributesList.stream()
.sorted(Comparator.comparingInt(s -> s.getDisplayName().length()))
.collect(Collectors.toList());
}

/**
* 元数据信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,21 @@ public class ${entity} {
<% var keyPropertyName; %>
<% /** -----------BEGIN 字段循环遍历----------- **/ %>
<% for(field in table.fields){ %>

<%
if(field.keyFlag){
keyPropertyName = field.propertyName;
}
%>
<% if(isNotEmpty(field.comment)){ %>

<% if(springdoc){ %>
@Schema(description = "${field.comment}")
<% }else if(swagger){ %>
@ApiModelProperty(value = "${field.comment}")
<% }else{ %>
<% if(useJavaDoc){ %>
/**
* ${field.comment}
*/
<% } %>
<% } %>
<% if(field.keyFlag){ %>
<%
/*主键*/
%>
<% if(field.keyIdentityFlag){ %>
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
<% }else if(isNotEmpty(idType)){ %>
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
<% }else if(field.convert){ %>
@TableId("${field.annotationColumnName}")
<% } %>
<%
/*普通字段*/
%>
<% }else if(isNotEmpty(field.fill)){ %>
<% if(field.convert){ %>
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
<% }else{ %>
@TableField(fill = FieldFill.${field.fill})
<% } %>
<% }else if(field.convert){ %>
@TableField("${field.annotationColumnName}")
<% } %>
<%
/*乐观锁注解*/
%>
<% if(field.versionField){ %>
@Version
<% } %>
<%
/*逻辑删除注解*/
%>
<% if(field.logicDeleteField){ %>
@TableLogic
<% for(an in field.annotationAttributesList){ %>
${an.displayName}
<% } %>
private ${field.propertyType} ${field.propertyName};
<% } %>
Expand Down
Loading

0 comments on commit 82759f3

Please sign in to comment.