!145 代码生成器改动

Merge pull request !145 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-16 07:07:52 +00:00 committed by Gitee
commit 6efa1f3231
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 129 additions and 15 deletions

View File

@ -94,6 +94,13 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.11</version>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@ -56,6 +56,11 @@ public class EntityConfig {
*/ */
private boolean withLombok; private boolean withLombok;
/**
* Entity 是否使用 Swagger 注解
*/
private boolean withSwagger;
/** /**
* 实体类数据源 * 实体类数据源
*/ */
@ -151,6 +156,21 @@ public class EntityConfig {
return this; return this;
} }
/**
* 是否启用 Swagger
*/
public boolean isWithSwagger() {
return withSwagger;
}
/**
* 设置是否启用 Swagger
*/
public EntityConfig setWithSwagger(boolean withSwagger) {
this.withSwagger = withSwagger;
return this;
}
/** /**
* 获取实体类数据源 * 获取实体类数据源
*/ */

View File

@ -282,6 +282,20 @@ public class GlobalConfig {
getJavadocConfig().setTableCommentFormat(tableCommentFormat); getJavadocConfig().setTableCommentFormat(tableCommentFormat);
} }
/**
* @see JavadocConfig#getColumnCommentFormat()
*/
public Function<String, String> getColumnCommentFormat() {
return getJavadocConfig().getColumnCommentFormat();
}
/**
* @see JavadocConfig#setColumnCommentFormat(UnaryOperator)
*/
public void setColumnCommentFormat(UnaryOperator<String> columnCommentFormat) {
getJavadocConfig().setColumnCommentFormat(columnCommentFormat);
}
/** /**
* @see JavadocConfig#getEntityPackage() * @see JavadocConfig#getEntityPackage()
*/ */
@ -877,6 +891,20 @@ public class GlobalConfig {
getEntityConfig().setWithLombok(entityWithLombok); getEntityConfig().setWithLombok(entityWithLombok);
} }
/**
* @see EntityConfig#isWithSwagger()
*/
public boolean isEntityWithSwagger() {
return getEntityConfig().isWithSwagger();
}
/**
* @see EntityConfig#setWithSwagger(boolean)
*/
public void setEntityWithSwagger(boolean entityWithSwagger) {
getEntityConfig().setWithSwagger(entityWithSwagger);
}
/** /**
* @see EntityConfig#getDataSource() * @see EntityConfig#getDataSource()
*/ */

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.config; package com.mybatisflex.codegen.config;
import com.mybatisflex.core.util.StringUtil;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.function.Function; import java.util.function.Function;
@ -46,6 +48,11 @@ public class JavadocConfig {
*/ */
private UnaryOperator<String> tableCommentFormat = UnaryOperator.identity(); private UnaryOperator<String> tableCommentFormat = UnaryOperator.identity();
/**
* 列名格式化
*/
private UnaryOperator<String> columnCommentFormat = UnaryOperator.identity();
/** /**
* Entity 包注释 * Entity 包注释
*/ */
@ -115,6 +122,9 @@ public class JavadocConfig {
} }
public String formatTableComment(String comment) { public String formatTableComment(String comment) {
if (StringUtil.isBlank(comment)) {
return "";
}
return tableCommentFormat.apply(comment); return tableCommentFormat.apply(comment);
} }
@ -133,6 +143,28 @@ public class JavadocConfig {
return this; return this;
} }
public String formatColumnComment(String comment) {
if (StringUtil.isBlank(comment)) {
return "";
}
return columnCommentFormat.apply(comment);
}
/**
* 获取列注释格式化
*/
public Function<String, String> getColumnCommentFormat() {
return columnCommentFormat;
}
/**
* 设置列注释格式化方案用来生成实体类属性注释
*/
public JavadocConfig setColumnCommentFormat(UnaryOperator<String> columnCommentFormat) {
this.columnCommentFormat = columnCommentFormat;
return this;
}
/** /**
* 获取实体类层包注释 * 获取实体类层包注释
*/ */

View File

@ -262,11 +262,7 @@ public class Column {
annotations.append("@ColumnMask(\"").append(columnConfig.getMask()).append("\")"); annotations.append("@ColumnMask(\"").append(columnConfig.getMask()).append("\")");
} }
String result = annotations.toString(); return annotations.toString();
if (!result.isEmpty()) {
result += "\n ";
}
return result;
} }
private void addComma(StringBuilder annotations, boolean needComma) { private void addComma(StringBuilder annotations, boolean needComma) {

View File

@ -114,12 +114,17 @@ public class Table {
} }
public List<Column> getColumns() { public List<Column> getColumns() {
// 生成字段排序
columns.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
.thenComparing(Column::getProperty));
return columns; return columns;
} }
public List<Column> getSortedColumns() {
ArrayList<Column> arrayList = new ArrayList<>(columns);
// 生成字段排序
arrayList.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
.thenComparing(Column::getProperty));
return arrayList;
}
public void setColumns(List<Column> columns) { public void setColumns(List<Column> columns) {
this.columns = columns; this.columns = columns;
} }

View File

@ -1,9 +1,14 @@
#set(withLombok = entityConfig.isWithLombok()) #set(withLombok = entityConfig.isWithLombok())
#set(withSwagger = entityConfig.isWithSwagger())
package #(packageConfig.entityPackage); package #(packageConfig.entityPackage);
#for(importClass : table.buildImports()) #for(importClass : table.buildImports())
import #(importClass); import #(importClass);
#end #end
#if(withSwagger)
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
#if(withLombok) #if(withLombok)
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
@ -23,12 +28,27 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
#end #end
#if(withSwagger)
@ApiModel("#(table.getComment())")
#end
#(table.buildTableAnnotation()) #(table.buildTableAnnotation())
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
#for(column : table.columns) #for(column : table.columns)
#(column.buildComment()) #set(comment = javadocConfig.formatColumnComment(column.comment))
#(column.buildAnnotations())private #(column.propertySimpleType) #(column.property); #if(isNotBlank(comment))
/**
* #(comment)
*/
#end
#set(annotations = column.buildAnnotations())
#if(isNotBlank(annotations))
#(annotations)
#end
#if(withSwagger)
@ApiModelProperty("#(column.comment)")
#end
private #(column.propertySimpleType) #(column.property);
#end #end
#if(!withLombok) #if(!withLombok)

View File

@ -18,7 +18,7 @@ public class #(tableDefClassName) extends TableDef {
*/ */
public static final #(tableDefClassName) #(tableDefConfig.buildFieldName(table.buildEntityClassName() + tableDefConfig.instanceSuffix)) = new #(tableDefClassName)(); public static final #(tableDefClassName) #(tableDefConfig.buildFieldName(table.buildEntityClassName() + tableDefConfig.instanceSuffix)) = new #(tableDefClassName)();
#for(column: table.columns) #for(column: table.getSortedColumns())
#(column.buildComment()) #(column.buildComment())
public final QueryColumn #(tableDefConfig.buildFieldName(column.property)) = new QueryColumn(this, "#(column.name)"); public final QueryColumn #(tableDefConfig.buildFieldName(column.property)) = new QueryColumn(this, "#(column.name)");

View File

@ -160,11 +160,14 @@ public class GeneratorTest {
//用户信息表用于存放用户信息 -> 用户信息 //用户信息表用于存放用户信息 -> 用户信息
UnaryOperator<String> tableFormat = (e) -> e.split("")[0].replace("", ""); UnaryOperator<String> tableFormat = (e) -> e.split("")[0].replace("", "");
//属性添加句号
UnaryOperator<String> columnFormat = (e) -> e.concat("");
//设置注解生成配置 //设置注解生成配置
globalConfig.getJavadocConfig() globalConfig.getJavadocConfig()
.setAuthor("王帅") .setAuthor("王帅")
.setTableCommentFormat(tableFormat); .setTableCommentFormat(tableFormat)
.setColumnCommentFormat(columnFormat);
//设置生成文件目录和根包 //设置生成文件目录和根包
globalConfig.getPackageConfig() globalConfig.getPackageConfig()
@ -197,11 +200,14 @@ public class GeneratorTest {
.setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE) .setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
.setOverwriteEnable(true); .setOverwriteEnable(true);
globalConfig.disableTableDef();
// 配置生成 entity // 配置生成 entity
globalConfig.enableEntity() globalConfig.enableEntity()
.setOverwriteEnable(true) .setOverwriteEnable(true)
.setDataSource("ds1") .setDataSource("ds1")
.setWithLombok(true); .setWithLombok(true)
.setWithSwagger(true);
// 配置生成 mapper // 配置生成 mapper
globalConfig.enableMapper() globalConfig.enableMapper()