feat: add "comment" for coedgen

This commit is contained in:
Michael Yang 2024-03-24 11:18:51 +08:00
parent f94438d9ef
commit a62faa0e5b
8 changed files with 76 additions and 12 deletions

View File

@ -109,6 +109,7 @@ public class Generator {
Table table = new Table();
table.setGlobalConfig(globalConfig);
table.setTableConfig(strategyConfig.getTableConfig(tableName));
table.setEntityConfig(globalConfig.getEntityConfig());
table.setSchema(schemaName);
table.setName(tableName);

View File

@ -112,6 +112,7 @@ public class ColumnConfig implements Serializable {
*/
private Boolean keyBefore;
public static ColumnConfig create() {
return new ColumnConfig();
}

View File

@ -111,6 +111,11 @@ public class EntityConfig implements Serializable {
*/
private String withBasePackage;
/**
* 是否支持把 comment 添加到 @column 注解里
*/
private boolean columnCommentEnable;
public String getSourceDir() {
return sourceDir;
@ -328,6 +333,14 @@ public class EntityConfig implements Serializable {
this.withBasePackage = withBasePackage;
}
public boolean isColumnCommentEnable() {
return columnCommentEnable;
}
public void setColumnCommentEnable(boolean columnCommentEnable) {
this.columnCommentEnable = columnCommentEnable;
}
public enum SwaggerVersion {
FOX("FOX"),

View File

@ -68,6 +68,7 @@ public class GlobalConfig implements Serializable {
private boolean mapperXmlGenerateEnable;
private boolean packageInfoGenerateEnable;
public GlobalConfig() {
this.javadocConfig = new JavadocConfig();
this.packageConfig = new PackageConfig();
@ -217,6 +218,7 @@ public class GlobalConfig implements Serializable {
packageInfoGenerateEnable = false;
}
// === 自定义配置 ===
public Object getCustomConfig(String key) {

View File

@ -72,6 +72,7 @@ public class TableConfig implements Serializable {
*/
private Map<String, ColumnConfig> columnConfigMap;
public static TableConfig create() {
return new TableConfig();
}

View File

@ -19,6 +19,7 @@ import com.mybatisflex.annotation.ColumnMask;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.core.util.StringUtil;
import java.util.LinkedHashSet;
@ -64,10 +65,10 @@ public class Column {
*/
private boolean isAutoIncrement;
/**
* 是否需要生成 @Column 注解
*/
private boolean needGenColumnAnnotation = false;
// /**
// * 是否需要生成 @Column 注解
// */
// private boolean needGenColumnAnnotation = false;
/**
* 数据库的字段类型比如 varchar/tinyint
@ -84,6 +85,8 @@ public class Column {
*/
private ColumnConfig columnConfig;
private EntityConfig entityConfig;
public String getName() {
return name;
}
@ -91,7 +94,6 @@ public class Column {
public void setName(String name) {
this.name = name;
this.property = buildPropertyName();
this.needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property));
}
public String getProperty() {
@ -177,6 +179,14 @@ public class Column {
this.columnConfig = columnConfig;
}
public EntityConfig getEntityConfig() {
return entityConfig;
}
public void setEntityConfig(EntityConfig entityConfig) {
this.entityConfig = entityConfig;
}
public String getterMethod() {
return "get" + StringUtil.firstCharToUpperCase(property);
}
@ -225,6 +235,14 @@ public class Column {
if (columnConfig.getKeyBefore() != null) {
addComma(annotations, needComma);
annotations.append("before = ").append(columnConfig.getKeyBefore());
needComma = true;
}
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
addComma(annotations, needComma);
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
annotations.append("comment = \"" + comment + "\"");
}
if (annotations.length() == 4) {
@ -234,6 +252,9 @@ public class Column {
}
}
boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment) && annotations.length() == 0);
//@Column 注解
if (columnConfig.getOnInsertValue() != null
|| columnConfig.getOnUpdateValue() != null
@ -247,7 +268,7 @@ public class Column {
) {
annotations.append("@Column(");
boolean needComma = false;
if (needGenColumnAnnotation) {
if (!name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
annotations.append("value = \"").append(name).append("\"");
needComma = true;
}
@ -290,6 +311,12 @@ public class Column {
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
addComma(annotations, needComma);
annotations.append("tenantId = true");
needComma = true;
}
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
addComma(annotations, needComma);
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
annotations.append("comment = \"" + comment + "\"");
}
annotations.append(")");
}
@ -336,6 +363,9 @@ public class Column {
addImportClass(importClasses, columnConfig.getTypeHandler().getName());
}
boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment));
if (columnConfig.getOnInsertValue() != null
|| columnConfig.getOnUpdateValue() != null
|| columnConfig.getLarge() != null

View File

@ -57,6 +57,8 @@ public class Table {
*/
private TableConfig tableConfig;
private EntityConfig entityConfig;
/**
* 全局配置
*/
@ -175,6 +177,7 @@ public class Table {
}
column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName()));
column.setEntityConfig(globalConfig.getEntityConfig());
columns.add(column);
}
@ -195,6 +198,14 @@ public class Table {
this.tableConfig = tableConfig;
}
public EntityConfig getEntityConfig() {
return entityConfig;
}
public void setEntityConfig(EntityConfig entityConfig) {
this.entityConfig = entityConfig;
}
// ===== 构建实体类文件 =====
/**
@ -277,6 +288,7 @@ public class Table {
tableAnnotation.append(", dataSource = \"").append(dataSource).append("\"");
}
if (tableConfig != null) {
if (StringUtil.isNotBlank(tableConfig.getSchema())) {
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
@ -297,6 +309,14 @@ public class Table {
tableAnnotation.append(", mapperGenerateEnable = false");
}
}
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
tableAnnotation.append(", comment = \"" + comment + "\"");
}
return tableAnnotation.append(")\n").toString();
}

View File

@ -17,13 +17,8 @@
package com.mybatisflex.codegen.test;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.config.TableDefConfig;
import com.mybatisflex.codegen.constant.TemplateConst;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import java.util.function.UnaryOperator;
@ -34,7 +29,7 @@ public class WithBaseGeneratorTest {
public void testCodeGen1() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flex_test?characterEncoding=utf-8");
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flex_test?characterEncoding=utf-8&useInformationSchema=true");
dataSource.setUsername("root");
dataSource.setPassword("123456");
@ -65,6 +60,7 @@ public class WithBaseGeneratorTest {
globalConfig.getEntityConfig().setWithBaseClassEnable(true);
globalConfig.getEntityConfig().setOverwriteEnable(true);
// globalConfig.getEntityConfig().setColumnCommentEnable(true);
// globalConfig.setEntityWithLombok(true);
globalConfig.setEntitySuperClass(BaseEntity.class);