From 50fd0fff807d3a71d5d192b8f7dc674fec490d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 24 Jun 2023 12:45:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20apt=20=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E5=85=B6=E9=85=8D=E7=BD=AE=E4=BB=8E=20`mybatis-flex.p?= =?UTF-8?q?roperties`=20=E7=A7=BB=E5=8A=A8=E5=88=B0=E6=A0=B9=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=9A=84=20`apt.config`;=20close=20#I7FHYB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/others/apt.md | 2 +- .../processor/config/MybatisFlexConfig.java | 32 ++++++------------- .../mybatis-flex.properties => apt.config} | 0 .../mybatis-flex.properties => apt.config} | 0 .../mybatis-flex.properties => apt.config} | 0 .../mybatis-flex-spring-test/apt.config | 2 ++ .../com/mybatisflex/test/AccountTest.java | 4 +-- .../test/resources/mybatis-flex.properties | 1 - 8 files changed, 14 insertions(+), 27 deletions(-) rename mybatis-flex-test/mybatis-flex-native-test/{src/main/resources/mybatis-flex.properties => apt.config} (100%) rename mybatis-flex-test/mybatis-flex-spring-boot-test/{src/main/resources/mybatis-flex.properties => apt.config} (100%) rename mybatis-flex-test/mybatis-flex-spring-cloud-test/{src/main/resources/mybatis-flex.properties => apt.config} (100%) create mode 100644 mybatis-flex-test/mybatis-flex-spring-test/apt.config delete mode 100644 mybatis-flex-test/mybatis-flex-spring-test/src/test/resources/mybatis-flex.properties diff --git a/docs/zh/others/apt.md b/docs/zh/others/apt.md index 739e6041..38a804ca 100644 --- a/docs/zh/others/apt.md +++ b/docs/zh/others/apt.md @@ -9,7 +9,7 @@ MyBatis-Flex 使用了 APT(Annotation Processing Tool)技术,在项目编 ## 配置文件和选项 -要对MyBatis-Flex 的APT细节选项进行配置,你需要在`resources`目录下创建名为`mybatis-flex.properties`的文件。 +要对 MyBatis-Flex 的 APT 细节选项进行配置,你需要在项目的 **根目录** 下创建名为 `apt.config` 的文件。 支持的配置选项如下: diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/config/MybatisFlexConfig.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/config/MybatisFlexConfig.java index ea5e92e8..d4f74178 100644 --- a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/config/MybatisFlexConfig.java +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/config/MybatisFlexConfig.java @@ -40,7 +40,7 @@ public class MybatisFlexConfig { /** * 配置文件名。 */ - private static final String MYBATIS_FLEX = "mybatis-flex.properties"; + private static final String APT_FILE_NAME = "apt.config"; /** * mybatis-flex.properties @@ -50,32 +50,18 @@ public class MybatisFlexConfig { public MybatisFlexConfig(Filer filer) { InputStream inputStream = null; try { - FileObject propertiesFileObject = filer.getResource(StandardLocation.CLASS_OUTPUT, "", MYBATIS_FLEX); + //target/classes/apt.config + FileObject aptConfigFileObject = filer.getResource(StandardLocation.CLASS_OUTPUT, "", APT_FILE_NAME); + String projectRootPath = FileUtil.getProjectRootPath(aptConfigFileObject.toUri().getPath()); - File propertiesFile = new File(propertiesFileObject.toUri()); - if (propertiesFile.exists()) { - inputStream = propertiesFileObject.openInputStream(); - } else if (getClass().getClassLoader().getResource(MYBATIS_FLEX) != null) { - inputStream = getClass().getClassLoader().getResourceAsStream(MYBATIS_FLEX); - } else { - File pomXmlFile = new File(propertiesFile.getParentFile().getParentFile().getParentFile(), "pom.xml"); - if (pomXmlFile.exists()) { - propertiesFile = new File(pomXmlFile.getParentFile(), "src/main/resources/mybatis-flex.properties"); - } + File aptConfigFile = new File(aptConfigFileObject.toUri()); + while (!aptConfigFile.exists() && !projectRootPath.equals(aptConfigFile.getParentFile().getAbsolutePath())) { + aptConfigFile = new File(aptConfigFile.getParentFile().getParentFile(), APT_FILE_NAME); } - if (inputStream == null && propertiesFile.exists()) { - inputStream = Files.newInputStream(propertiesFile.toPath()); - } - - // 兜底,如果还是没找到,就找项目根目录下的 mybatis-flex.properties - if (inputStream == null) { - final String projectRootPath = FileUtil.getProjectRootPath(propertiesFileObject.toUri().getPath()); - final File filePath = new File(projectRootPath, MYBATIS_FLEX); - if (filePath.exists()) { - inputStream = Files.newInputStream(filePath.toPath()); - } + if (aptConfigFile.exists()) { + inputStream = Files.newInputStream(aptConfigFile.toPath()); } if (inputStream != null) { diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/mybatis-flex.properties b/mybatis-flex-test/mybatis-flex-native-test/apt.config similarity index 100% rename from mybatis-flex-test/mybatis-flex-native-test/src/main/resources/mybatis-flex.properties rename to mybatis-flex-test/mybatis-flex-native-test/apt.config diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mybatis-flex.properties b/mybatis-flex-test/mybatis-flex-spring-boot-test/apt.config similarity index 100% rename from mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mybatis-flex.properties rename to mybatis-flex-test/mybatis-flex-spring-boot-test/apt.config diff --git a/mybatis-flex-test/mybatis-flex-spring-cloud-test/src/main/resources/mybatis-flex.properties b/mybatis-flex-test/mybatis-flex-spring-cloud-test/apt.config similarity index 100% rename from mybatis-flex-test/mybatis-flex-spring-cloud-test/src/main/resources/mybatis-flex.properties rename to mybatis-flex-test/mybatis-flex-spring-cloud-test/apt.config diff --git a/mybatis-flex-test/mybatis-flex-spring-test/apt.config b/mybatis-flex-test/mybatis-flex-spring-test/apt.config new file mode 100644 index 00000000..f84786d9 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-test/apt.config @@ -0,0 +1,2 @@ +processor.mappersGenerateEnable = false +processor.allInTables = true \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java index 1551e637..607ab5cd 100644 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-test/src/test/java/com/mybatisflex/test/AccountTest.java @@ -30,7 +30,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; -import static com.mybatisflex.test.model.table.Tables.account_def; +import static com.mybatisflex.test.model.table.Tables.ACCOUNT; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AppConfig.class) @@ -49,7 +49,7 @@ public class AccountTest implements WithAssertions { @Test public void testSelectByQuery() { QueryWrapper queryWrapper = QueryWrapper.create() - .where(account_def.age.eq(18)); + .where(ACCOUNT.AGE.eq(18)); List accounts = accountMapper.selectListByQuery(queryWrapper); assertThat(accounts.size()).isEqualTo(1); assertThat(accounts.get(0).getAge()).isEqualTo(18); diff --git a/mybatis-flex-test/mybatis-flex-spring-test/src/test/resources/mybatis-flex.properties b/mybatis-flex-test/mybatis-flex-spring-test/src/test/resources/mybatis-flex.properties deleted file mode 100644 index ec75864c..00000000 --- a/mybatis-flex-test/mybatis-flex-spring-test/src/test/resources/mybatis-flex.properties +++ /dev/null @@ -1 +0,0 @@ -processor.mappersGenerateEnable = true