diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml
index 469d4a9f..f7b69554 100644
--- a/mybatis-flex-codegen/pom.xml
+++ b/mybatis-flex-codegen/pom.xml
@@ -94,6 +94,13 @@
true
+
+ io.swagger
+ swagger-annotations
+ 1.6.11
+ true
+
+
junit
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java
index b36aaf87..8fc0753d 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java
@@ -56,6 +56,11 @@ public class EntityConfig {
*/
private boolean withLombok;
+ /**
+ * Entity 是否使用 Swagger 注解。
+ */
+ private boolean withSwagger;
+
/**
* 实体类数据源。
*/
@@ -151,6 +156,21 @@ public class EntityConfig {
return this;
}
+ /**
+ * 是否启用 Swagger。
+ */
+ public boolean isWithSwagger() {
+ return withSwagger;
+ }
+
+ /**
+ * 设置是否启用 Swagger。
+ */
+ public EntityConfig setWithSwagger(boolean withSwagger) {
+ this.withSwagger = withSwagger;
+ return this;
+ }
+
/**
* 获取实体类数据源。
*/
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java
index 42c2da0a..6f7633e9 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java
@@ -282,6 +282,20 @@ public class GlobalConfig {
getJavadocConfig().setTableCommentFormat(tableCommentFormat);
}
+ /**
+ * @see JavadocConfig#getColumnCommentFormat()
+ */
+ public Function getColumnCommentFormat() {
+ return getJavadocConfig().getColumnCommentFormat();
+ }
+
+ /**
+ * @see JavadocConfig#setColumnCommentFormat(UnaryOperator)
+ */
+ public void setColumnCommentFormat(UnaryOperator columnCommentFormat) {
+ getJavadocConfig().setColumnCommentFormat(columnCommentFormat);
+ }
+
/**
* @see JavadocConfig#getEntityPackage()
*/
@@ -877,6 +891,20 @@ public class GlobalConfig {
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()
*/
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java
index 2ba8beb4..a66edaa2 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java
@@ -16,6 +16,8 @@
package com.mybatisflex.codegen.config;
+import com.mybatisflex.core.util.StringUtil;
+
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.function.Function;
@@ -46,6 +48,11 @@ public class JavadocConfig {
*/
private UnaryOperator tableCommentFormat = UnaryOperator.identity();
+ /**
+ * 列名格式化。
+ */
+ private UnaryOperator columnCommentFormat = UnaryOperator.identity();
+
/**
* Entity 包注释。
*/
@@ -115,6 +122,9 @@ public class JavadocConfig {
}
public String formatTableComment(String comment) {
+ if (StringUtil.isBlank(comment)) {
+ return "";
+ }
return tableCommentFormat.apply(comment);
}
@@ -133,6 +143,28 @@ public class JavadocConfig {
return this;
}
+ public String formatColumnComment(String comment) {
+ if (StringUtil.isBlank(comment)) {
+ return "";
+ }
+ return columnCommentFormat.apply(comment);
+ }
+
+ /**
+ * 获取列注释格式化。
+ */
+ public Function getColumnCommentFormat() {
+ return columnCommentFormat;
+ }
+
+ /**
+ * 设置列注释格式化方案,用来生成实体类属性注释。
+ */
+ public JavadocConfig setColumnCommentFormat(UnaryOperator columnCommentFormat) {
+ this.columnCommentFormat = columnCommentFormat;
+ return this;
+ }
+
/**
* 获取实体类层包注释。
*/
@@ -225,4 +257,4 @@ public class JavadocConfig {
return this;
}
-}
\ No newline at end of file
+}
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java
index 4544f55b..4f2c9cd1 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java
@@ -262,11 +262,7 @@ public class Column {
annotations.append("@ColumnMask(\"").append(columnConfig.getMask()).append("\")");
}
- String result = annotations.toString();
- if (!result.isEmpty()) {
- result += "\n ";
- }
- return result;
+ return annotations.toString();
}
private void addComma(StringBuilder annotations, boolean needComma) {
diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java
index d57bb2b6..af81310d 100644
--- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java
+++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java
@@ -114,12 +114,17 @@ public class Table {
}
public List getColumns() {
- // 生成字段排序
- columns.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
- .thenComparing(Column::getProperty));
return columns;
}
+ public List getSortedColumns() {
+ ArrayList arrayList = new ArrayList<>(columns);
+ // 生成字段排序
+ arrayList.sort(Comparator.comparingInt((Column c) -> c.getProperty().length())
+ .thenComparing(Column::getProperty));
+ return arrayList;
+ }
+
public void setColumns(List columns) {
this.columns = columns;
}
diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl
index 02b40fe3..e4e683f5 100644
--- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl
+++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl
@@ -1,9 +1,14 @@
#set(withLombok = entityConfig.isWithLombok())
+#set(withSwagger = entityConfig.isWithSwagger())
package #(packageConfig.entityPackage);
#for(importClass : table.buildImports())
import #(importClass);
#end
+#if(withSwagger)
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+#end
#if(withLombok)
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -23,12 +28,27 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
#end
+#if(withSwagger)
+@ApiModel("#(table.getComment())")
+#end
#(table.buildTableAnnotation())
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
#for(column : table.columns)
- #(column.buildComment())
- #(column.buildAnnotations())private #(column.propertySimpleType) #(column.property);
+ #set(comment = javadocConfig.formatColumnComment(column.comment))
+ #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
#if(!withLombok)
diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl
index 018ec960..f7353103 100644
--- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl
+++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl
@@ -18,7 +18,7 @@ public class #(tableDefClassName) extends TableDef {
*/
public static final #(tableDefClassName) #(tableDefConfig.buildFieldName(table.buildEntityClassName() + tableDefConfig.instanceSuffix)) = new #(tableDefClassName)();
-#for(column: table.columns)
+#for(column: table.getSortedColumns())
#(column.buildComment())
public final QueryColumn #(tableDefConfig.buildFieldName(column.property)) = new QueryColumn(this, "#(column.name)");
diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java
index 9a651a81..b2ce21e1 100644
--- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java
+++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java
@@ -85,7 +85,7 @@ public class GeneratorTest {
generator.generate();
}
- // @Test
+ // @Test
public void testCodeGen2() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
@@ -160,11 +160,14 @@ public class GeneratorTest {
//用户信息表,用于存放用户信息。 -> 用户信息
UnaryOperator tableFormat = (e) -> e.split(",")[0].replace("表", "");
+ //属性添加句号
+ UnaryOperator columnFormat = (e) -> e.concat("。");
//设置注解生成配置
globalConfig.getJavadocConfig()
.setAuthor("王帅")
- .setTableCommentFormat(tableFormat);
+ .setTableCommentFormat(tableFormat)
+ .setColumnCommentFormat(columnFormat);
//设置生成文件目录和根包
globalConfig.getPackageConfig()
@@ -197,11 +200,14 @@ public class GeneratorTest {
.setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
.setOverwriteEnable(true);
+ globalConfig.disableTableDef();
+
// 配置生成 entity
globalConfig.enableEntity()
.setOverwriteEnable(true)
.setDataSource("ds1")
- .setWithLombok(true);
+ .setWithLombok(true)
+ .setWithSwagger(true);
// 配置生成 mapper
globalConfig.enableMapper()