mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
fixed globalConfig.setEntityWithLombok(true) Not working;add multi tablePrefix config support
This commit is contained in:
parent
42dbdefd5b
commit
8fab35034e
@ -57,6 +57,14 @@
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.26</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
||||
@ -102,6 +102,7 @@ public class Generator {
|
||||
|
||||
|
||||
private void buildTableColumns(Table table) throws SQLException {
|
||||
|
||||
Map<String, String> columnRemarks = buildColumnRemarks(table);
|
||||
|
||||
String sql = dialect.forBuildColumns(table.getName());
|
||||
|
||||
@ -33,7 +33,7 @@ public class GlobalConfig {
|
||||
private String entityPackage;
|
||||
|
||||
//entity 是否使用 Lombok
|
||||
private Boolean entityWithLombok = false;
|
||||
private boolean entityWithLombok = false;
|
||||
|
||||
//是否生成 mapper 类
|
||||
private boolean mapperGenerateEnable = false;
|
||||
@ -41,7 +41,7 @@ public class GlobalConfig {
|
||||
//mapper 的包名
|
||||
private String mapperPackage;
|
||||
|
||||
//数据库表前缀
|
||||
//数据库表前缀,多个前缀用英文逗号(,) 隔开
|
||||
private String tablePrefix;
|
||||
|
||||
//逻辑删除的默认字段名称
|
||||
@ -91,11 +91,11 @@ public class GlobalConfig {
|
||||
this.entityPackage = entityPackage.trim();
|
||||
}
|
||||
|
||||
public Boolean getEntityWithLombok() {
|
||||
public boolean isEntityWithLombok() {
|
||||
return entityWithLombok;
|
||||
}
|
||||
|
||||
public void setEntityWithLombok(Boolean entityWithLombok) {
|
||||
public void setEntityWithLombok(boolean entityWithLombok) {
|
||||
this.entityWithLombok = entityWithLombok;
|
||||
}
|
||||
|
||||
|
||||
@ -99,6 +99,18 @@ public class Table {
|
||||
imports.addAll(column.getImportClasses());
|
||||
}
|
||||
|
||||
//开启 lombok
|
||||
if (globalConfig.isEntityWithLombok()) {
|
||||
//import lombok.AllArgsConstructor;
|
||||
//import lombok.Builder;
|
||||
//import lombok.Data;
|
||||
//import lombok.NoArgsConstructor;
|
||||
imports.add("lombok.AllArgsConstructor");
|
||||
imports.add("lombok.Builder");
|
||||
imports.add("lombok.Data");
|
||||
imports.add("lombok.NoArgsConstructor");
|
||||
}
|
||||
|
||||
if (tableConfig != null) {
|
||||
if (tableConfig.getInsertListenerClass() != null) {
|
||||
imports.add(tableConfig.getInsertListenerClass().getName());
|
||||
@ -120,8 +132,15 @@ public class Table {
|
||||
public String buildEntityClassName() {
|
||||
String entityJavaFileName = name;
|
||||
String tablePrefix = globalConfig.getTablePrefix();
|
||||
if (tablePrefix != null && name.startsWith(tablePrefix)) {
|
||||
entityJavaFileName = name.substring(tablePrefix.length());
|
||||
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 StringUtil.firstCharToUpperCase(StringUtil.underlineToCamel(entityJavaFileName));
|
||||
}
|
||||
@ -130,8 +149,19 @@ public class Table {
|
||||
* 构建 @Table(...) 注解
|
||||
*/
|
||||
public String buildTableAnnotation() {
|
||||
StringBuilder tableAnnotation = new StringBuilder("@Table(");
|
||||
tableAnnotation.append("value = \"").append(name).append("\"");
|
||||
StringBuilder tableAnnotation = new StringBuilder();
|
||||
if (globalConfig.isEntityWithLombok()) {
|
||||
//@Data
|
||||
//@Builder
|
||||
//@NoArgsConstructor
|
||||
//@AllArgsConstructor
|
||||
tableAnnotation.append("@Data\n");
|
||||
tableAnnotation.append("@Builder\n");
|
||||
tableAnnotation.append("@NoArgsConstructor\n");
|
||||
tableAnnotation.append("@AllArgsConstructor\n");
|
||||
}
|
||||
|
||||
tableAnnotation.append("@Table(value = \"").append(name).append("\"");
|
||||
|
||||
if (tableConfig != null) {
|
||||
if (tableConfig.getSchema() != null) {
|
||||
|
||||
@ -13,6 +13,7 @@ public class #(table.buildEntityClassName()) {
|
||||
|
||||
#end
|
||||
|
||||
#if(!globalConfig.isEntityWithLombok())
|
||||
#for(column: table.columns)
|
||||
public #(column.propertySimpleType) #(column.buildGetter())() {
|
||||
return #(column.property);
|
||||
@ -23,4 +24,5 @@ public class #(table.buildEntityClassName()) {
|
||||
}
|
||||
|
||||
#end
|
||||
#end
|
||||
}
|
||||
|
||||
@ -35,12 +35,14 @@ public class GeneratorTest {
|
||||
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
|
||||
// globalConfig.setTablePrefix("tb_");
|
||||
// globalConfig.setEntityWithLombok(true);
|
||||
|
||||
//设置只生成哪些表
|
||||
globalConfig.addGenerateTable("account", "account_session");
|
||||
globalConfig.addGenerateTable("tb_account", "account_session");
|
||||
|
||||
//设置 entity 的包名
|
||||
globalConfig.setEntityPackage("com.test.test");
|
||||
globalConfig.setEntityPackage("com.test.entity");
|
||||
|
||||
//是否生成 mapper 类,默认为 false
|
||||
globalConfig.setMapperGenerateEnable(true);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user