mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feature:代码生成器添加配置 entity 前后缀配置的功能
This commit is contained in:
parent
46376023c5
commit
dec39e7045
@ -64,7 +64,7 @@ public class Generator {
|
||||
|
||||
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
|
||||
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
|
||||
globalConfig.getEntityClassPrefix() + table.buildEntityClassName() + globalConfig.getEntityClassSuffix() + ".java");
|
||||
table.buildEntityClassName() + ".java");
|
||||
if (!entityJavaFile.getParentFile().exists()) {
|
||||
if (!entityJavaFile.getParentFile().mkdirs()) {
|
||||
throw new IllegalStateException("Can not mkdirs by dir: " + entityJavaFile.getParentFile());
|
||||
@ -78,8 +78,8 @@ public class Generator {
|
||||
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
|
||||
|
||||
|
||||
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" + globalConfig.getMapperClassPrefix()
|
||||
+ table.buildEntityClassName() + globalConfig.getMapperClassSuffix() + ".java");
|
||||
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" +
|
||||
table.buildMapperClassName() + ".java");
|
||||
if (!mapperJavaFile.getParentFile().exists()) {
|
||||
if (!mapperJavaFile.getParentFile().mkdirs()) {
|
||||
throw new IllegalStateException("Can not mkdirs by dir: " + mapperJavaFile.getParentFile());
|
||||
|
||||
@ -33,10 +33,10 @@ public class GlobalConfig {
|
||||
private String entityPackage;
|
||||
|
||||
//mapper 类的前缀
|
||||
private String entityClassPrefix = "";
|
||||
private String entityClassPrefix;
|
||||
|
||||
//mapper 类的后缀
|
||||
private String entityClassSuffix = "";
|
||||
private String entityClassSuffix;
|
||||
|
||||
//entity 是否使用 Lombok
|
||||
private boolean entityWithLombok = false;
|
||||
@ -107,6 +107,9 @@ public class GlobalConfig {
|
||||
}
|
||||
|
||||
public String getEntityClassPrefix() {
|
||||
if (StringUtil.isBlank(entityClassPrefix)) {
|
||||
return "";
|
||||
}
|
||||
return entityClassPrefix;
|
||||
}
|
||||
|
||||
@ -115,6 +118,9 @@ public class GlobalConfig {
|
||||
}
|
||||
|
||||
public String getEntityClassSuffix() {
|
||||
if (StringUtil.isBlank(entityClassSuffix)) {
|
||||
return "";
|
||||
}
|
||||
return entityClassSuffix;
|
||||
}
|
||||
|
||||
@ -147,7 +153,7 @@ public class GlobalConfig {
|
||||
}
|
||||
|
||||
public String getMapperClassPrefix() {
|
||||
if (StringUtil.isBlank(mapperClassPrefix)){
|
||||
if (StringUtil.isBlank(mapperClassPrefix)) {
|
||||
return "";
|
||||
}
|
||||
return mapperClassPrefix;
|
||||
|
||||
@ -163,7 +163,28 @@ public class Table {
|
||||
}
|
||||
}
|
||||
}
|
||||
return StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName));
|
||||
return globalConfig.getEntityClassPrefix()
|
||||
+ StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName))
|
||||
+ globalConfig.getEntityClassSuffix();
|
||||
}
|
||||
|
||||
|
||||
public String buildMapperClassName() {
|
||||
String entityJavaFileName = 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)) {
|
||||
entityJavaFileName = name.substring(trimPrefix.length());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return globalConfig.getMapperClassPrefix()
|
||||
+ StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName))
|
||||
+ globalConfig.getMapperClassSuffix();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -6,7 +6,7 @@ import #(importClass);
|
||||
|
||||
|
||||
#(table.buildTableAnnotation())
|
||||
public class #(globalConfig.entityClassPrefix ??)#(table.buildEntityClassName())#(globalConfig.entityClassSuffix ??) {
|
||||
public class #(table.buildEntityClassName()) {
|
||||
|
||||
#for(column: table.columns) #(column.buildAnnotations())
|
||||
private #(column.propertySimpleType) #(column.property);
|
||||
|
||||
@ -3,6 +3,6 @@ package #(globalConfig.mapperPackage);
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
|
||||
|
||||
public interface #(globalConfig.mapperClassPrefix ??)#(table.buildEntityClassName())#(globalConfig.mapperClassSuffix ??) extends BaseMapper<#(table.buildEntityClassName())> {
|
||||
public interface #(table.buildMapperClassName()) extends BaseMapper<#(table.buildEntityClassName())> {
|
||||
|
||||
}
|
||||
|
||||
@ -43,9 +43,13 @@ public class GeneratorTest {
|
||||
|
||||
//设置 entity 的包名
|
||||
globalConfig.setEntityPackage("com.test.entity");
|
||||
globalConfig.setEntityClassPrefix("My");
|
||||
globalConfig.setEntityClassSuffix("Entity");
|
||||
|
||||
//是否生成 mapper 类,默认为 false
|
||||
globalConfig.setMapperGenerateEnable(true);
|
||||
globalConfig.setMapperClassPrefix("Flex");
|
||||
globalConfig.setMapperClassSuffix("Dao");
|
||||
|
||||
//设置 mapper 类的包名
|
||||
globalConfig.setMapperPackage("com.test.mapper");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user