mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
feat: add "comment" for coedgen
This commit is contained in:
parent
f94438d9ef
commit
a62faa0e5b
@ -109,6 +109,7 @@ public class Generator {
|
|||||||
Table table = new Table();
|
Table table = new Table();
|
||||||
table.setGlobalConfig(globalConfig);
|
table.setGlobalConfig(globalConfig);
|
||||||
table.setTableConfig(strategyConfig.getTableConfig(tableName));
|
table.setTableConfig(strategyConfig.getTableConfig(tableName));
|
||||||
|
table.setEntityConfig(globalConfig.getEntityConfig());
|
||||||
|
|
||||||
table.setSchema(schemaName);
|
table.setSchema(schemaName);
|
||||||
table.setName(tableName);
|
table.setName(tableName);
|
||||||
|
|||||||
@ -112,6 +112,7 @@ public class ColumnConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private Boolean keyBefore;
|
private Boolean keyBefore;
|
||||||
|
|
||||||
|
|
||||||
public static ColumnConfig create() {
|
public static ColumnConfig create() {
|
||||||
return new ColumnConfig();
|
return new ColumnConfig();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -111,6 +111,11 @@ public class EntityConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private String withBasePackage;
|
private String withBasePackage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否支持把 comment 添加到 @column 注解里
|
||||||
|
*/
|
||||||
|
private boolean columnCommentEnable;
|
||||||
|
|
||||||
|
|
||||||
public String getSourceDir() {
|
public String getSourceDir() {
|
||||||
return sourceDir;
|
return sourceDir;
|
||||||
@ -328,6 +333,14 @@ public class EntityConfig implements Serializable {
|
|||||||
this.withBasePackage = withBasePackage;
|
this.withBasePackage = withBasePackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isColumnCommentEnable() {
|
||||||
|
return columnCommentEnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumnCommentEnable(boolean columnCommentEnable) {
|
||||||
|
this.columnCommentEnable = columnCommentEnable;
|
||||||
|
}
|
||||||
|
|
||||||
public enum SwaggerVersion {
|
public enum SwaggerVersion {
|
||||||
|
|
||||||
FOX("FOX"),
|
FOX("FOX"),
|
||||||
|
|||||||
@ -68,6 +68,7 @@ public class GlobalConfig implements Serializable {
|
|||||||
private boolean mapperXmlGenerateEnable;
|
private boolean mapperXmlGenerateEnable;
|
||||||
private boolean packageInfoGenerateEnable;
|
private boolean packageInfoGenerateEnable;
|
||||||
|
|
||||||
|
|
||||||
public GlobalConfig() {
|
public GlobalConfig() {
|
||||||
this.javadocConfig = new JavadocConfig();
|
this.javadocConfig = new JavadocConfig();
|
||||||
this.packageConfig = new PackageConfig();
|
this.packageConfig = new PackageConfig();
|
||||||
@ -217,6 +218,7 @@ public class GlobalConfig implements Serializable {
|
|||||||
packageInfoGenerateEnable = false;
|
packageInfoGenerateEnable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// === 自定义配置 ===
|
// === 自定义配置 ===
|
||||||
|
|
||||||
public Object getCustomConfig(String key) {
|
public Object getCustomConfig(String key) {
|
||||||
|
|||||||
@ -72,6 +72,7 @@ public class TableConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private Map<String, ColumnConfig> columnConfigMap;
|
private Map<String, ColumnConfig> columnConfigMap;
|
||||||
|
|
||||||
|
|
||||||
public static TableConfig create() {
|
public static TableConfig create() {
|
||||||
return new TableConfig();
|
return new TableConfig();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import com.mybatisflex.annotation.ColumnMask;
|
|||||||
import com.mybatisflex.annotation.Id;
|
import com.mybatisflex.annotation.Id;
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
import com.mybatisflex.codegen.config.ColumnConfig;
|
import com.mybatisflex.codegen.config.ColumnConfig;
|
||||||
|
import com.mybatisflex.codegen.config.EntityConfig;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
@ -64,10 +65,10 @@ public class Column {
|
|||||||
*/
|
*/
|
||||||
private boolean isAutoIncrement;
|
private boolean isAutoIncrement;
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* 是否需要生成 @Column 注解。
|
// * 是否需要生成 @Column 注解。
|
||||||
*/
|
// */
|
||||||
private boolean needGenColumnAnnotation = false;
|
// private boolean needGenColumnAnnotation = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库的字段类型,比如 varchar/tinyint 等
|
* 数据库的字段类型,比如 varchar/tinyint 等
|
||||||
@ -84,6 +85,8 @@ public class Column {
|
|||||||
*/
|
*/
|
||||||
private ColumnConfig columnConfig;
|
private ColumnConfig columnConfig;
|
||||||
|
|
||||||
|
private EntityConfig entityConfig;
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -91,7 +94,6 @@ public class Column {
|
|||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.property = buildPropertyName();
|
this.property = buildPropertyName();
|
||||||
this.needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProperty() {
|
public String getProperty() {
|
||||||
@ -177,6 +179,14 @@ public class Column {
|
|||||||
this.columnConfig = columnConfig;
|
this.columnConfig = columnConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityConfig getEntityConfig() {
|
||||||
|
return entityConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEntityConfig(EntityConfig entityConfig) {
|
||||||
|
this.entityConfig = entityConfig;
|
||||||
|
}
|
||||||
|
|
||||||
public String getterMethod() {
|
public String getterMethod() {
|
||||||
return "get" + StringUtil.firstCharToUpperCase(property);
|
return "get" + StringUtil.firstCharToUpperCase(property);
|
||||||
}
|
}
|
||||||
@ -225,6 +235,14 @@ public class Column {
|
|||||||
if (columnConfig.getKeyBefore() != null) {
|
if (columnConfig.getKeyBefore() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(annotations, needComma);
|
||||||
annotations.append("before = ").append(columnConfig.getKeyBefore());
|
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) {
|
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 注解
|
//@Column 注解
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
|| columnConfig.getOnUpdateValue() != null
|
|| columnConfig.getOnUpdateValue() != null
|
||||||
@ -247,7 +268,7 @@ public class Column {
|
|||||||
) {
|
) {
|
||||||
annotations.append("@Column(");
|
annotations.append("@Column(");
|
||||||
boolean needComma = false;
|
boolean needComma = false;
|
||||||
if (needGenColumnAnnotation) {
|
if (!name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
||||||
annotations.append("value = \"").append(name).append("\"");
|
annotations.append("value = \"").append(name).append("\"");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
@ -290,6 +311,12 @@ public class Column {
|
|||||||
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
||||||
addComma(annotations, needComma);
|
addComma(annotations, needComma);
|
||||||
annotations.append("tenantId = true");
|
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(")");
|
annotations.append(")");
|
||||||
}
|
}
|
||||||
@ -336,6 +363,9 @@ public class Column {
|
|||||||
addImportClass(importClasses, columnConfig.getTypeHandler().getName());
|
addImportClass(importClasses, columnConfig.getTypeHandler().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||||
|
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment));
|
||||||
|
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
|| columnConfig.getOnUpdateValue() != null
|
|| columnConfig.getOnUpdateValue() != null
|
||||||
|| columnConfig.getLarge() != null
|
|| columnConfig.getLarge() != null
|
||||||
|
|||||||
@ -57,6 +57,8 @@ public class Table {
|
|||||||
*/
|
*/
|
||||||
private TableConfig tableConfig;
|
private TableConfig tableConfig;
|
||||||
|
|
||||||
|
private EntityConfig entityConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 全局配置。
|
* 全局配置。
|
||||||
*/
|
*/
|
||||||
@ -175,6 +177,7 @@ public class Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName()));
|
column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName()));
|
||||||
|
column.setEntityConfig(globalConfig.getEntityConfig());
|
||||||
|
|
||||||
columns.add(column);
|
columns.add(column);
|
||||||
}
|
}
|
||||||
@ -195,6 +198,14 @@ public class Table {
|
|||||||
this.tableConfig = tableConfig;
|
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("\"");
|
tableAnnotation.append(", dataSource = \"").append(dataSource).append("\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (tableConfig != null) {
|
if (tableConfig != null) {
|
||||||
if (StringUtil.isNotBlank(tableConfig.getSchema())) {
|
if (StringUtil.isNotBlank(tableConfig.getSchema())) {
|
||||||
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
|
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
|
||||||
@ -297,6 +309,14 @@ public class Table {
|
|||||||
tableAnnotation.append(", mapperGenerateEnable = false");
|
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();
|
return tableAnnotation.append(")\n").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,13 +17,8 @@
|
|||||||
package com.mybatisflex.codegen.test;
|
package com.mybatisflex.codegen.test;
|
||||||
|
|
||||||
import com.mybatisflex.codegen.Generator;
|
import com.mybatisflex.codegen.Generator;
|
||||||
import com.mybatisflex.codegen.config.ColumnConfig;
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
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 com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
@ -34,7 +29,7 @@ public class WithBaseGeneratorTest {
|
|||||||
public void testCodeGen1() {
|
public void testCodeGen1() {
|
||||||
//配置数据源
|
//配置数据源
|
||||||
HikariDataSource dataSource = new HikariDataSource();
|
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.setUsername("root");
|
||||||
dataSource.setPassword("123456");
|
dataSource.setPassword("123456");
|
||||||
|
|
||||||
@ -65,6 +60,7 @@ public class WithBaseGeneratorTest {
|
|||||||
|
|
||||||
globalConfig.getEntityConfig().setWithBaseClassEnable(true);
|
globalConfig.getEntityConfig().setWithBaseClassEnable(true);
|
||||||
globalConfig.getEntityConfig().setOverwriteEnable(true);
|
globalConfig.getEntityConfig().setOverwriteEnable(true);
|
||||||
|
// globalConfig.getEntityConfig().setColumnCommentEnable(true);
|
||||||
|
|
||||||
// globalConfig.setEntityWithLombok(true);
|
// globalConfig.setEntityWithLombok(true);
|
||||||
globalConfig.setEntitySuperClass(BaseEntity.class);
|
globalConfig.setEntitySuperClass(BaseEntity.class);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user