新增生成tableDef,使用apt有时候还是很别扭,建议关闭

This commit is contained in:
闵柳华 2023-05-04 23:33:56 +08:00
parent c54e321429
commit a26815f7d4
7 changed files with 109 additions and 2 deletions

View File

@ -73,6 +73,16 @@ public class Generator {
templateEngine.generateEntity(globalConfig, table, entityJavaFile); templateEngine.generateEntity(globalConfig, table, entityJavaFile);
String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java");
if (!tableDefJavaFile.getParentFile().exists()) {
if (!tableDefJavaFile.getParentFile().mkdirs()) {
throw new IllegalStateException("Can not mkdirs by dir: " + tableDefJavaFile.getParentFile());
}
}
templateEngine.generateTableDef(globalConfig, table, tableDefJavaFile);
if (globalConfig.isMapperGenerateEnable()) { if (globalConfig.isMapperGenerateEnable()) {
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/"); String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");

View File

@ -49,6 +49,15 @@ public class GlobalConfig {
//entity 是否使用 Lombok //entity 是否使用 Lombok
private boolean entityWithLombok = false; private boolean entityWithLombok = false;
//tableDef 的包名
private String tableDefPackage;
//tableDef 类的前缀
private String tableDefClassPrefix;
//tableDef 类的后缀
private String tableDefClassSuffix;
//是否生成 mapper //是否生成 mapper
private boolean mapperGenerateEnable = false; private boolean mapperGenerateEnable = false;
@ -163,6 +172,30 @@ public class GlobalConfig {
this.entityWithLombok = entityWithLombok; this.entityWithLombok = entityWithLombok;
} }
public String getTableDefPackage() {
return tableDefPackage;
}
public void setTableDefPackage(String tableDefPackage) {
this.tableDefPackage = tableDefPackage;
}
public String getTableDefClassPrefix() {
return tableDefClassPrefix;
}
public void setTableDefClassPrefix(String tableDefClassPrefix) {
this.tableDefClassPrefix = tableDefClassPrefix;
}
public String getTableDefClassSuffix() {
return tableDefClassSuffix;
}
public void setTableDefClassSuffix(String tableDefClassSuffix) {
this.tableDefClassSuffix = tableDefClassSuffix;
}
public boolean isMapperGenerateEnable() { public boolean isMapperGenerateEnable() {
return mapperGenerateEnable; return mapperGenerateEnable;
} }

View File

@ -188,6 +188,29 @@ public class Table {
+ globalConfig.getEntityClassSuffix(); + globalConfig.getEntityClassSuffix();
} }
/**
* 构建 tableDef Class 名称
*
* @return className
*/
public String buildTableDefClassName() {
String tableDefJavaFileName = name;
String tablePrefix = globalConfig.getTablePrefix();
if (tablePrefix != null) {
String[] tablePrefixes = tablePrefix.split(",");
for (String prefix : tablePrefixes) {
String trimPrefix = prefix.trim();
if (trimPrefix.length() > 0 && name.startsWith(trimPrefix)) {
tableDefJavaFileName = name.substring(trimPrefix.length());
break;
}
}
}
return globalConfig.getTableDefClassPrefix()
+ StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(tableDefJavaFileName))
+ globalConfig.getTableDefClassSuffix();
}
public String buildExtends() { public String buildExtends() {
if (globalConfig.getEntitySupperClass() != null) { if (globalConfig.getEntitySupperClass() != null) {
return " extends " + globalConfig.getEntitySupperClass().getSimpleName(); return " extends " + globalConfig.getEntitySupperClass().getSimpleName();

View File

@ -48,6 +48,18 @@ public class EnjoyTemplate implements ITemplate {
System.out.println("Entity has been generated: " + entityJavaFile.getAbsolutePath()); System.out.println("Entity has been generated: " + entityJavaFile.getAbsolutePath());
} }
@Override
public void generateTableDef(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("globalConfig", globalConfig);
params.put("table", table);
FileOutputStream fileOutputStream = new FileOutputStream(entityJavaFile);
engine.getTemplate("/templates/enjoy/tableDef.tpl").render(params, fileOutputStream);
System.out.println("TableDef has been generated: " + entityJavaFile.getAbsolutePath());
}
@Override @Override
public void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception { public void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception {

View File

@ -24,5 +24,7 @@ public interface ITemplate {
void generateEntity(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception ; void generateEntity(GlobalConfig globalConfig, Table table, File entityJavaFile) throws Exception ;
void generateTableDef(GlobalConfig globalConfig, Table table, File tableDefJavaFile) throws Exception ;
void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception ; void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception ;
} }

View File

@ -0,0 +1,21 @@
package #(globalConfig.entityPackage).tables;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.table.TableDef;
public class #(table.buildTableDefClassName()) extends TableDef {
public static final #(table.buildTableDefClassName()) #(table.name) = new #(table.buildTableDefClassName())("#(table.name)");
#for(column: table.columns)
public QueryColumn #(column.name) = new QueryColumn(this, "#(column.name)");
#end
public QueryColumn[] default_columns = new QueryColumn[]{#for(column: table.columns) #if(!column.name.equals("del_flag"))#(column.name)#if(for.index + 1 != for.size),#end#end#end};
public QueryColumn[] all_columns = new QueryColumn[]{#for(column: table.columns) #(column.name)#if(for.index + 1 != for.size),#end#end};
public #(table.buildTableDefClassName())(String tableName) {
super(tableName);
}
}

View File

@ -20,15 +20,16 @@ import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig; import com.mybatisflex.codegen.config.TableConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
public class GeneratorTest { public class GeneratorTest {
// @Test @Test
public void testGenerator() { public void testGenerator() {
//配置数据源 //配置数据源
HikariDataSource dataSource = new HikariDataSource(); HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/jbootadmin?characterEncoding=utf-8"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hh-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowMultiQueries=true");
dataSource.setUsername("root"); dataSource.setUsername("root");
dataSource.setPassword("123456"); dataSource.setPassword("123456");
@ -50,6 +51,11 @@ public class GeneratorTest {
globalConfig.setEntityClassPrefix("My"); globalConfig.setEntityClassPrefix("My");
globalConfig.setEntityClassSuffix("Entity"); globalConfig.setEntityClassSuffix("Entity");
//设置 entity 的包名
globalConfig.setTableDefPackage("com.test.entity.tables");
globalConfig.setTableDefClassPrefix("My");
globalConfig.setTableDefClassSuffix("TableDef");
//是否生成 mapper 默认为 false //是否生成 mapper 默认为 false
globalConfig.setMapperGenerateEnable(true); globalConfig.setMapperGenerateEnable(true);
globalConfig.setMapperClassPrefix("Flex"); globalConfig.setMapperClassPrefix("Flex");