From d071a0701234009972ad959ce6e10ae3fe28f140 Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Sun, 16 Jul 2023 12:20:23 +0800
Subject: [PATCH 1/5] build: add swagger-annotations
---
mybatis-flex-codegen/pom.xml | 7 +++++++
1 file changed, 7 insertions(+)
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
From 2440e420847f2de505b8180de21e29e38911bc0f Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Sun, 16 Jul 2023 12:24:57 +0800
Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E5=8F=AA=E6=9C=89=E8=BE=85=E5=8A=A9?=
=?UTF-8?q?=E7=B1=BB=E5=AD=97=E6=AE=B5=E6=89=8D=E4=BC=9A=E6=8E=92=E5=BA=8F?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/mybatisflex/codegen/entity/Table.java | 11 ++++++++---
.../src/main/resources/templates/enjoy/tableDef.tpl | 2 +-
2 files changed, 9 insertions(+), 4 deletions(-)
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/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)");
From e5f5250738383f052c5066a62bbd852cd5b8c251 Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Sun, 16 Jul 2023 12:33:38 +0800
Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=88=97?=
=?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=A0=BC=E5=BC=8F=E5=8C=96=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../codegen/config/JavadocConfig.java | 34 ++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
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
+}
From b91701de033ae0a2274c48e34588cf201b68078d Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Sun, 16 Jul 2023 12:34:23 +0800
Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20swagger=20?=
=?UTF-8?q?=E6=B3=A8=E8=A7=A3=E6=94=AF=E6=8C=81=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../codegen/config/EntityConfig.java | 20 +++++++++++++
.../codegen/config/GlobalConfig.java | 28 +++++++++++++++++++
.../mybatisflex/codegen/entity/Column.java | 6 +---
.../main/resources/templates/enjoy/entity.tpl | 24 ++++++++++++++--
4 files changed, 71 insertions(+), 7 deletions(-)
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/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/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)
From 2f07b1c059d9d995753867d6d9846b7aa36d23fa Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Sun, 16 Jul 2023 14:35:39 +0800
Subject: [PATCH 5/5] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/mybatisflex/codegen/test/GeneratorTest.java | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
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()