mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
commit
cddf0af9e8
@ -66,19 +66,10 @@ public class EntityConfig {
|
|||||||
*/
|
*/
|
||||||
private SwaggerVersion swaggerVersion;
|
private SwaggerVersion swaggerVersion;
|
||||||
|
|
||||||
public enum SwaggerVersion {
|
/**
|
||||||
FOX("FOX"),
|
* Entity 是否启用 Active Record 功能。
|
||||||
DOC("DOC");
|
*/
|
||||||
private final String name;
|
private boolean withActiveRecord;
|
||||||
|
|
||||||
SwaggerVersion(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实体类数据源。
|
* 实体类数据源。
|
||||||
@ -207,6 +198,21 @@ public class EntityConfig {
|
|||||||
return this;
|
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;
|
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);
|
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()
|
* @see EntityConfig#getDataSource()
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,11 +1,16 @@
|
|||||||
#set(withLombok = entityConfig.isWithLombok())
|
#set(withLombok = entityConfig.isWithLombok())
|
||||||
#set(withSwagger = entityConfig.isWithSwagger())
|
#set(withSwagger = entityConfig.isWithSwagger())
|
||||||
#set(swaggerVersion = entityConfig.getSwaggerVersion())
|
#set(swaggerVersion = entityConfig.getSwaggerVersion())
|
||||||
|
#set(withActiveRecord = entityConfig.isWithActiveRecord())
|
||||||
|
#set(entityClassName = table.buildEntityClassName())
|
||||||
package #(packageConfig.entityPackage);
|
package #(packageConfig.entityPackage);
|
||||||
|
|
||||||
#for(importClass : table.buildImports())
|
#for(importClass : table.buildImports())
|
||||||
import #(importClass);
|
import #(importClass);
|
||||||
#end
|
#end
|
||||||
|
#if(withActiveRecord)
|
||||||
|
import com.mybatisflex.core.activerecord.Model;
|
||||||
|
#end
|
||||||
#if(withSwagger && swaggerVersion.getName() == "FOX")
|
#if(withSwagger && swaggerVersion.getName() == "FOX")
|
||||||
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModel;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
@ -14,11 +19,17 @@ import io.swagger.annotations.ApiModelProperty;
|
|||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
#end
|
#end
|
||||||
#if(withLombok)
|
#if(withLombok)
|
||||||
|
#if(withActiveRecord)
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
#else
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
#end
|
#end
|
||||||
|
#end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* #(table.getComment()) 实体类。
|
* #(table.getComment()) 实体类。
|
||||||
@ -27,11 +38,17 @@ import lombok.NoArgsConstructor;
|
|||||||
* @since #(javadocConfig.getSince())
|
* @since #(javadocConfig.getSince())
|
||||||
*/
|
*/
|
||||||
#if(withLombok)
|
#if(withLombok)
|
||||||
|
#if(withActiveRecord)
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@Data(staticConstructor = "create")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
#else
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
#end
|
#end
|
||||||
|
#end
|
||||||
#if(withSwagger && swaggerVersion.getName() == "FOX")
|
#if(withSwagger && swaggerVersion.getName() == "FOX")
|
||||||
@ApiModel("#(table.getComment())")
|
@ApiModel("#(table.getComment())")
|
||||||
#end
|
#end
|
||||||
@ -39,7 +56,7 @@ import lombok.NoArgsConstructor;
|
|||||||
@Schema(description = "#(table.getComment())")
|
@Schema(description = "#(table.getComment())")
|
||||||
#end
|
#end
|
||||||
#(table.buildTableAnnotation())
|
#(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)
|
#for(column : table.columns)
|
||||||
#set(comment = javadocConfig.formatColumnComment(column.comment))
|
#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;
|
private #(column.propertySimpleType) #(column.property)#if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#end;
|
||||||
|
|
||||||
#end
|
#end
|
||||||
|
|
||||||
#if(!withLombok)
|
#if(!withLombok)
|
||||||
|
#if(withActiveRecord)
|
||||||
|
public static #(entityClassName) create() {
|
||||||
|
return new #(entityClassName)();
|
||||||
|
}
|
||||||
|
|
||||||
|
#end
|
||||||
#for(column: table.columns)
|
#for(column: table.columns)
|
||||||
public #(column.propertySimpleType) #(column.getterMethod())() {
|
public #(column.propertySimpleType) #(column.getterMethod())() {
|
||||||
return #(column.property);
|
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)) {
|
public void #(column.setterMethod())(#(column.propertySimpleType) #(column.property)) {
|
||||||
this.#(column.property) = #(column.property);
|
this.#(column.property) = #(column.property);
|
||||||
}
|
}
|
||||||
|
#end
|
||||||
|
|
||||||
#end
|
#end
|
||||||
#end}
|
#end}
|
||||||
|
|||||||
@ -226,4 +226,46 @@ public class GeneratorTest {
|
|||||||
return globalConfig;
|
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.paginate.Page;
|
||||||
import com.mybatisflex.core.util.SqlUtil;
|
import com.mybatisflex.core.util.SqlUtil;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ -33,7 +34,7 @@ import java.util.Optional;
|
|||||||
@SuppressWarnings({"unused", "unchecked"})
|
@SuppressWarnings({"unused", "unchecked"})
|
||||||
public abstract class Model<T extends Model<T>>
|
public abstract class Model<T extends Model<T>>
|
||||||
extends QueryModel<T>
|
extends QueryModel<T>
|
||||||
implements MapperModel<T> {
|
implements MapperModel<T>, Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据实体类构建的条件删除数据。
|
* 根据实体类构建的条件删除数据。
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user