fix java代码生成器base类缺少import;feat kotlin代码生成器支持withBaseClass模式

This commit is contained in:
rainy 2024-06-04 13:56:30 +08:00
parent 74b6185f1f
commit b22fcf75e7
5 changed files with 55 additions and 15 deletions

View File

@ -364,11 +364,11 @@ public class Table {
/** /**
* 构建 extends 继承 * 构建 extends 继承
*/ */
public String buildExtends() { public String buildExtends(boolean isBase) {
EntityConfig entityConfig = globalConfig.getEntityConfig(); EntityConfig entityConfig = globalConfig.getEntityConfig();
Class<?> superClass = entityConfig.getSuperClass(this); Class<?> superClass = entityConfig.getSuperClass(this);
if (superClass != null) { if (superClass != null) {
return " extends " + superClass.getSimpleName()+(entityConfig.isSuperClassGenericity()?("<"+buildEntityClassName()+">"):""); return " extends " + superClass.getSimpleName()+(entityConfig.isSuperClassGenericity()?("<"+buildEntityClassName()+(isBase?entityConfig.getWithBaseClassSuffix():"")+">"):"");
} else { } else {
return ""; return "";
} }
@ -389,14 +389,14 @@ public class Table {
/** /**
* 构建 kt 继承 * 构建 kt 继承
*/ */
public String buildKtExtends(){ public String buildKtExtends(boolean isBase){
EntityConfig entityConfig = globalConfig.getEntityConfig(); EntityConfig entityConfig = globalConfig.getEntityConfig();
Class<?> superClass = entityConfig.getSuperClass(this); Class<?> superClass = entityConfig.getSuperClass(this);
List<String> s = new ArrayList<>(); List<String> s = new ArrayList<>();
if (superClass != null) { if (superClass != null) {
String name = superClass.getSimpleName(); String name = superClass.getSimpleName();
if (entityConfig.isSuperClassGenericity()){ if (entityConfig.isSuperClassGenericity()){
name+="<"+buildEntityClassName()+">"; name+="<"+buildEntityClassName()+(isBase?entityConfig.getWithBaseClassSuffix():"")+">";
} }
name+="()"; name+="()";
s.add(name); s.add(name);

View File

@ -38,6 +38,7 @@ public class EntityGenerator implements IGenerator {
protected String templatePath; protected String templatePath;
protected String entityWithBaseTemplatePath = "/templates/enjoy/entityWithBase.tpl"; protected String entityWithBaseTemplatePath = "/templates/enjoy/entityWithBase.tpl";
protected String ktEntityWithBaseTemplatePath = "/templates/enjoy/entityWithBase.kotlin.tpl";
public EntityGenerator() { public EntityGenerator() {
@ -98,11 +99,12 @@ public class EntityGenerator implements IGenerator {
// 开启生成 baseClass // 开启生成 baseClass
if (entityConfig.isWithBaseClassEnable()) { if (entityConfig.isWithBaseClassEnable()) {
if (globalConfig.getFileType() == GlobalConfig.FileType.KOTLIN) { if (globalConfig.getFileType() == GlobalConfig.FileType.KOTLIN) {
throw new UnsupportedOperationException("暂不支持 Kotlin 生成 WithBaseClass 模式。"); // throw new UnsupportedOperationException("暂不支持 Kotlin 生成 WithBaseClass 模式。");
templatePath = this.ktEntityWithBaseTemplatePath;
}else{
templatePath = this.entityWithBaseTemplatePath;
} }
templatePath = this.entityWithBaseTemplatePath;
String baseClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix(); String baseClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();
params.put("baseClassName", baseClassName); params.put("baseClassName", baseClassName);
@ -136,7 +138,7 @@ public class EntityGenerator implements IGenerator {
String baseEntityClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix(); String baseEntityClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();
File baseEntityJavaFile = new File(sourceDir, baseEntityPackagePath + "/" + baseEntityClassName + ".java"); File baseEntityJavaFile = new File(sourceDir, baseEntityPackagePath + "/" + baseEntityClassName + globalConfig.getFileType());
// 排除忽略列 // 排除忽略列

View File

@ -1,7 +1,7 @@
#set(withSwagger = entityConfig.isWithSwagger()) #set(withSwagger = entityConfig.isWithSwagger())
#set(withActiveRecord = entityConfig.isWithActiveRecord()) #set(withActiveRecord = entityConfig.isWithActiveRecord())
package #(entityPackageName); package #(entityPackageName)
#for(importClass : table.buildImports(isBase)) #for(importClass : table.buildImports(isBase))
import #(importClass) import #(importClass)
@ -18,6 +18,7 @@ import io.swagger.annotations.ApiModelProperty
#if(withSwagger && swaggerVersion.getName() == "DOC") #if(withSwagger && swaggerVersion.getName() == "DOC")
import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.media.Schema
#end #end
#if(!isBase)
/** /**
* #(table.getComment()) 实体类。 * #(table.getComment()) 实体类。
* *
@ -25,7 +26,8 @@ import io.swagger.v3.oas.annotations.media.Schema
* @since #(javadocConfig.getSince()) * @since #(javadocConfig.getSince())
*/ */
#(table.buildTableAnnotation()) #(table.buildTableAnnotation())
class #(entityClassName) #if(withActiveRecord) : Model<#(entityClassName)>()#else#(table.buildKtExtends())#end { #end
#if(isBase)open #end class #(entityClassName) #if(withActiveRecord) : Model<#(entityClassName)>()#else#(table.buildKtExtends(isBase))#end {
#for(column : table.columns) #for(column : table.columns)
#set(comment = javadocConfig.formatColumnComment(column.comment)) #set(comment = javadocConfig.formatColumnComment(column.comment))
#if(isNotBlank(comment)) #if(isNotBlank(comment))
@ -43,7 +45,7 @@ class #(entityClassName) #if(withActiveRecord) : Model<#(entityClassName)>()#els
#if(withSwagger && swaggerVersion.getName() == "DOC") #if(withSwagger && swaggerVersion.getName() == "DOC")
@Schema(description = "#(column.comment)") @Schema(description = "#(column.comment)")
#end #end
var #(column.property): #(column.propertySimpleType)? = #if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#else null#end #if(isBase)open #end var #(column.property): #(column.propertySimpleType)? = #if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#else null#end
#end #end
} }

View File

@ -12,6 +12,10 @@ import #(importClass);
import com.mybatisflex.core.activerecord.Model; import com.mybatisflex.core.activerecord.Model;
#end #end
#if(jdkVersion >= 14)
import java.io.Serial;
#end
#if(!isBase) #if(!isBase)
#if(withSwagger && swaggerVersion.getName() == "FOX") #if(withSwagger && swaggerVersion.getName() == "FOX")
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
@ -35,9 +39,6 @@ import lombok.EqualsAndHashCode;
#end #end
#end #end
#end #end
#if(jdkVersion >= 14)
import java.io.Serial;
#end
/** /**
* #(table.getComment()) 实体类。 * #(table.getComment()) 实体类。
@ -67,7 +68,7 @@ import java.io.Serial;
@Schema(description = "#(table.getComment())") @Schema(description = "#(table.getComment())")
#end #end
#(table.buildTableAnnotation()) #end #(table.buildTableAnnotation()) #end
public class #(entityClassName)#if(withActiveRecord) extends Model<#(entityClassName)>#else#(table.buildExtends())#(table.buildImplements())#end { public class #(entityClassName)#if(withActiveRecord) extends Model<#(entityClassName)>#else#(table.buildExtends(isBase))#(table.buildImplements())#end {
#if(jdkVersion >= 14) #if(jdkVersion >= 14)
@Serial @Serial

View File

@ -0,0 +1,35 @@
#set(withSwagger = entityConfig.isWithSwagger())
#set(withActiveRecord = entityConfig.isWithActiveRecord())
package #(entityPackageName)
#for(importClass : table.buildImports(isBase))
import #(importClass)
#end
import #(baseClassPackage).#(baseClassName);
#if(withActiveRecord)
import com.mybatisflex.core.activerecord.Model
#end
#if(withSwagger && swaggerVersion.getName() == "FOX")
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
#end
#if(withSwagger && swaggerVersion.getName() == "DOC")
import io.swagger.v3.oas.annotations.media.Schema
#end
/**
* #(table.getComment()) 实体类。
*
* @author #(javadocConfig.getAuthor())
* @since #(javadocConfig.getSince())
*/
#if(withSwagger && swaggerVersion.getName() == "FOX")
@ApiModel("#(table.getComment())")
#end
#if(withSwagger && swaggerVersion.getName() == "DOC")
@Schema(description = "#(table.getComment())")
#end
#if(!isBase)#(table.buildTableAnnotation())#end
class #(entityClassName) : #(baseClassName)()