mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
cddf0af9e8
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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()
|
||||
*/
|
||||
|
||||
@ -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}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
/**
|
||||
* 根据实体类构建的条件删除数据。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user