mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge branch 'main' of https://gitee.com/mybatis-flex/mybatis-flex
This commit is contained in:
commit
090faf8cdf
@ -73,6 +73,16 @@ public class Generator {
|
||||
|
||||
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()) {
|
||||
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
|
||||
|
||||
@ -49,6 +49,15 @@ public class GlobalConfig {
|
||||
//entity 是否使用 Lombok
|
||||
private boolean entityWithLombok = false;
|
||||
|
||||
//tableDef 的包名
|
||||
private String tableDefPackage;
|
||||
|
||||
//tableDef 类的前缀
|
||||
private String tableDefClassPrefix;
|
||||
|
||||
//tableDef 类的后缀
|
||||
private String tableDefClassSuffix;
|
||||
|
||||
//是否生成 mapper 类
|
||||
private boolean mapperGenerateEnable = false;
|
||||
|
||||
@ -163,6 +172,30 @@ public class GlobalConfig {
|
||||
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() {
|
||||
return mapperGenerateEnable;
|
||||
}
|
||||
|
||||
@ -20,7 +20,13 @@ import com.mybatisflex.codegen.config.TableConfig;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Comparator;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Table {
|
||||
@ -188,6 +194,29 @@ public class Table {
|
||||
+ 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() {
|
||||
if (globalConfig.getEntitySupperClass() != null) {
|
||||
return " extends " + globalConfig.getEntitySupperClass().getSimpleName();
|
||||
|
||||
@ -48,6 +48,18 @@ public class EnjoyTemplate implements ITemplate {
|
||||
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
|
||||
public void generateMapper(GlobalConfig globalConfig, Table table, File mapperJavaFile) throws Exception {
|
||||
|
||||
@ -24,5 +24,7 @@ public interface ITemplate {
|
||||
|
||||
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 ;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -20,15 +20,16 @@ import com.mybatisflex.codegen.config.ColumnConfig;
|
||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||
import com.mybatisflex.codegen.config.TableConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GeneratorTest {
|
||||
|
||||
|
||||
// @Test
|
||||
@Test
|
||||
public void testGenerator() {
|
||||
//配置数据源
|
||||
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.setPassword("123456");
|
||||
|
||||
@ -50,6 +51,11 @@ public class GeneratorTest {
|
||||
globalConfig.setEntityClassPrefix("My");
|
||||
globalConfig.setEntityClassSuffix("Entity");
|
||||
|
||||
//设置 entity 的包名
|
||||
globalConfig.setTableDefPackage("com.test.entity.tables");
|
||||
globalConfig.setTableDefClassPrefix("My");
|
||||
globalConfig.setTableDefClassSuffix("TableDef");
|
||||
|
||||
//是否生成 mapper 类,默认为 false
|
||||
globalConfig.setMapperGenerateEnable(true);
|
||||
globalConfig.setMapperClassPrefix("Flex");
|
||||
|
||||
@ -81,6 +81,30 @@ public class ArrayUtil {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 可变长参形式数组
|
||||
*
|
||||
* @param first 第一个数组
|
||||
* @param second 第二个数组
|
||||
* @param <T>
|
||||
* @return 新的数组
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> T[] append(T[] first, T... second) {
|
||||
if (first == null && second == null) {
|
||||
throw new IllegalArgumentException("not allow first and second are null.");
|
||||
} else if (isEmpty(first) && second != null) {
|
||||
return second;
|
||||
} else if (isEmpty(second)) {
|
||||
return first;
|
||||
} else {
|
||||
T[] result = Arrays.copyOf(first, first.length + second.length);
|
||||
System.arraycopy(second, 0, result, first.length, second.length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看数组中是否包含某一个值
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user