!204 feat:代码生成器支持生成 Active Record

Merge pull request !204 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-29 01:40:49 +00:00 committed by Gitee
commit cddf0af9e8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 125 additions and 17 deletions

View File

@ -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;
}
}
}

View File

@ -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()
*/

View File

@ -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}

View File

@ -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<String> 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();
}
}

View File

@ -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<T extends Model<T>>
extends QueryModel<T>
implements MapperModel<T> {
implements MapperModel<T>, Serializable {
/**
* 根据实体类构建的条件删除数据