From 9f0bb84c432a3279c7ea6c164ea670a1eb443027 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 28 Jul 2023 20:47:49 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20Model=20=E5=AE=9E=E7=8E=B0=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/mybatisflex/core/activerecord/Model.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/Model.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/Model.java index ac1b7f1a..93ad42a8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/Model.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/Model.java @@ -20,6 +20,7 @@ import com.mybatisflex.core.activerecord.query.QueryModel; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.util.SqlUtil; +import java.io.Serializable; import java.util.List; import java.util.Optional; @@ -33,7 +34,7 @@ import java.util.Optional; @SuppressWarnings({"unused", "unchecked"}) public abstract class Model> extends QueryModel - implements MapperModel { + implements MapperModel, Serializable { /** * 根据实体类构建的条件删除数据。 From a38bb0bcfe7910f0e753bc81d72aa7cffbf3ee64 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 28 Jul 2023 21:26:04 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20Active=20Recor?= =?UTF-8?q?d=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/EntityConfig.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 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 c8dd9b96..6636419f 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 @@ -66,19 +66,10 @@ public class EntityConfig { */ private SwaggerVersion swaggerVersion; - public enum SwaggerVersion { - FOX("FOX"), - DOC("DOC"); - private final String name; - - SwaggerVersion(String name) { - this.name = name; - } - - public String getName() { - return this.name; - } - } + /** + * Entity 是否启用 Active Record 功能。 + */ + private boolean withActiveRecord; /** * 实体类数据源。 @@ -207,6 +198,21 @@ public class EntityConfig { return this; } + /** + * 是否启用 Active Record。 + */ + public boolean isWithActiveRecord() { + return withActiveRecord; + } + + /** + * 设置是否启用 Active Record。 + */ + public EntityConfig setWithActiveRecord(boolean withActiveRecord) { + this.withActiveRecord = withActiveRecord; + return this; + } + /** * 获取实体类数据源。 */ @@ -222,4 +228,20 @@ public class EntityConfig { return this; } + public enum SwaggerVersion { + + FOX("FOX"), + DOC("DOC"); + private final String name; + + SwaggerVersion(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + } + } From 13ea70f2052cd83572c79ab2fddb02bd86c17b7b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 28 Jul 2023 21:26:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/codegen/config/GlobalConfig.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 39eed1ad..d7252e6c 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 @@ -912,6 +912,20 @@ public class GlobalConfig { getEntityConfig().setWithSwagger(entityWithSwagger); } + /** + * @see EntityConfig#isWithActiveRecord() + */ + public boolean isWithActiveRecord() { + return getEntityConfig().isWithActiveRecord(); + } + + /** + * @see EntityConfig#setWithActiveRecord(boolean) + */ + public void setWithActiveRecord(boolean withActiveRecord) { + getEntityConfig().setWithActiveRecord(withActiveRecord); + } + /** * @see EntityConfig#getDataSource() */ From cf8ae0c4d4286a436a026951781f86cd8b1ad251 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 28 Jul 2023 21:26:38 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20Entity=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Active=20Record=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/templates/enjoy/entity.tpl | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) 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 cb79e4e5..d570c86b 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -1,11 +1,16 @@ #set(withLombok = entityConfig.isWithLombok()) #set(withSwagger = entityConfig.isWithSwagger()) #set(swaggerVersion = entityConfig.getSwaggerVersion()) +#set(withActiveRecord = entityConfig.isWithActiveRecord()) +#set(entityClassName = table.buildEntityClassName()) package #(packageConfig.entityPackage); #for(importClass : table.buildImports()) import #(importClass); #end +#if(withActiveRecord) +import com.mybatisflex.core.activerecord.Model; +#end #if(withSwagger && swaggerVersion.getName() == "FOX") import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -14,11 +19,17 @@ import io.swagger.annotations.ApiModelProperty; import io.swagger.v3.oas.annotations.media.Schema; #end #if(withLombok) +#if(withActiveRecord) +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; +#else import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; #end +#end /** * #(table.getComment()) 实体类。 @@ -27,11 +38,17 @@ import lombok.NoArgsConstructor; * @since #(javadocConfig.getSince()) */ #if(withLombok) +#if(withActiveRecord) +@Accessors(chain = true) +@Data(staticConstructor = "create") +@EqualsAndHashCode(callSuper = true) +#else @Data @Builder @NoArgsConstructor @AllArgsConstructor #end +#end #if(withSwagger && swaggerVersion.getName() == "FOX") @ApiModel("#(table.getComment())") #end @@ -39,7 +56,7 @@ import lombok.NoArgsConstructor; @Schema(description = "#(table.getComment())") #end #(table.buildTableAnnotation()) -public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { +public class #(entityClassName)#if(withActiveRecord) extends Model<#(entityClassName)>#else#(table.buildExtends())#(table.buildImplements())#end { #for(column : table.columns) #set(comment = javadocConfig.formatColumnComment(column.comment)) @@ -61,16 +78,28 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build private #(column.propertySimpleType) #(column.property)#if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#end; #end - #if(!withLombok) + #if(withActiveRecord) + public static #(entityClassName) create() { + return new #(entityClassName)(); + } + + #end #for(column: table.columns) public #(column.propertySimpleType) #(column.getterMethod())() { return #(column.property); } + #if(withActiveRecord) + public #(entityClassName) #(column.setterMethod())(#(column.propertySimpleType) #(column.property)) { + this.#(column.property) = #(column.property); + return this; + } + #else public void #(column.setterMethod())(#(column.propertySimpleType) #(column.property)) { this.#(column.property) = #(column.property); } + #end #end #end} From 7170086004afc8e0a84e27c69ccfeac7f086d535 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 28 Jul 2023 21:26:46 +0800 Subject: [PATCH 5/5] =?UTF-8?q?test:=20Entity=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Active=20Record=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/test/GeneratorTest.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) 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 aa350c86..e8891d4e 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 @@ -86,7 +86,7 @@ public class GeneratorTest { generator.generate(); } - // @Test + // @Test public void testCodeGen2() { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); @@ -226,4 +226,46 @@ public class GeneratorTest { return globalConfig; } + @Test + public void testCodeGen4() { + //配置数据源 + HikariDataSource dataSource = new HikariDataSource(); + dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"); + dataSource.setUsername("root"); + dataSource.setPassword("12345678"); + + GlobalConfig globalConfig = new GlobalConfig(); + + //用户信息表,用于存放用户信息。 -> 用户信息 + UnaryOperator tableFormat = (e) -> e.split(",")[0].replace("表", ""); + + //设置注解生成配置 + globalConfig.getJavadocConfig() + .setAuthor("王帅") + .setTableCommentFormat(tableFormat); + + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setMapperXmlPath(System.getProperty("user.dir") + "/src/test/resources/mapper") + .setBasePackage("com.test"); + + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("sys_") + .setGenerateTable("sys_user"); + + //配置生成 entity + globalConfig.enableEntity() + .setOverwriteEnable(true) + .setWithLombok(false) + .setWithActiveRecord(true); + + //通过 datasource 和 globalConfig 创建代码生成器 + Generator generator = new Generator(dataSource, globalConfig); + + //开始生成代码 + generator.generate(); + } + }