From dec39e70454faa52bc970bec303e1f13c8505ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 13 Apr 2023 17:35:06 +0800 Subject: [PATCH] =?UTF-8?q?feature=EF=BC=9A=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AE=20entity?= =?UTF-8?q?=20=E5=89=8D=E5=90=8E=E7=BC=80=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/Generator.java | 6 ++--- .../codegen/config/GlobalConfig.java | 12 +++++++--- .../com/mybatisflex/codegen/entity/Table.java | 23 ++++++++++++++++++- .../main/resources/templates/enjoy/entity.tpl | 2 +- .../main/resources/templates/enjoy/mapper.tpl | 2 +- .../codegen/test/GeneratorTest.java | 4 ++++ 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java index b0b35bca..b6abb05e 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java @@ -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()); diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index 23b7c4be..e385a791 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -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; diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index a4064d42..a86cf131 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -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(); } /** diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl index ba06e149..fb1de060 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -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); diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl index 996a34fa..8901a05f 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl @@ -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())> { } diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java index 0798ab74..53edbeb5 100644 --- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java @@ -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");