close #I6VQ5W

This commit is contained in:
开源海哥 2023-04-14 15:22:31 +08:00
parent f4cbf0c396
commit 11764532b5
5 changed files with 69 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import com.mybatisflex.codegen.template.EnjoyTemplate;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.core.util.StringUtil;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -38,6 +39,12 @@ public class GlobalConfig {
//entity 类的后缀
private String entityClassSuffix;
//entity 类的父类可以自定义一些 BaseEntity
private Class<?> entitySupperClass;
//entity 默认实现的接口
private Class<?>[] entityInterfaces = {Serializable.class};
//entity 是否使用 Lombok
private boolean entityWithLombok = false;
@ -128,6 +135,22 @@ public class GlobalConfig {
this.entityClassSuffix = entityClassSuffix;
}
public Class<?> getEntitySupperClass() {
return entitySupperClass;
}
public void setEntitySupperClass(Class<?> entitySupperClass) {
this.entitySupperClass = entitySupperClass;
}
public Class<?>[] getEntityInterfaces() {
return entityInterfaces;
}
public void setEntityInterfaces(Class<?>[] entityInterfaces) {
this.entityInterfaces = entityInterfaces;
}
public boolean isEntityWithLombok() {
return entityWithLombok;
}

View File

@ -129,6 +129,16 @@ public class Table {
imports.add("lombok.NoArgsConstructor");
}
if (globalConfig.getEntitySupperClass() != null) {
imports.add(globalConfig.getEntitySupperClass().getName());
}
if (globalConfig.getEntityInterfaces() != null) {
for (Class<?> entityInterface : globalConfig.getEntityInterfaces()) {
imports.add(entityInterface.getName());
}
}
if (tableConfig != null) {
if (tableConfig.getInsertListenerClass() != null) {
imports.add(tableConfig.getInsertListenerClass().getName());
@ -168,6 +178,24 @@ public class Table {
+ globalConfig.getEntityClassSuffix();
}
public String buildExtends() {
if (globalConfig.getEntitySupperClass() != null) {
return " extends " + globalConfig.getEntitySupperClass().getSimpleName();
} else {
return "";
}
}
public String buildImplements() {
Class<?>[] entityInterfaces = globalConfig.getEntityInterfaces();
if (entityInterfaces != null && entityInterfaces.length > 0) {
return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces)
.map(Class::getSimpleName).collect(Collectors.toList()));
} else {
return "";
}
}
public String buildMapperClassName() {
String entityJavaFileName = name;

View File

@ -6,7 +6,7 @@ import #(importClass);
#(table.buildTableAnnotation())
public class #(table.buildEntityClassName()) {
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
#for(column: table.columns) #(column.buildAnnotations())
private #(column.propertySimpleType) #(column.property);

View File

@ -0,0 +1,15 @@
package com.mybatisflex.codegen.test;
import java.util.Date;
public class BaseEntity {
private Date createTime;
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}

View File

@ -38,6 +38,8 @@ public class GeneratorTest {
// globalConfig.setTablePrefix("tb_");
// globalConfig.setEntityWithLombok(true);
globalConfig.setEntitySupperClass(BaseEntity.class);
//设置只生成哪些表
globalConfig.addGenerateTable("tb_account", "account_session");