重构 apt 功能,其配置从 mybatis-flex.properties 移动到根目录的 apt.config; close #I7FHYB

This commit is contained in:
开源海哥 2023-06-24 12:45:59 +08:00
parent e47500779f
commit 50fd0fff80
8 changed files with 14 additions and 27 deletions

View File

@ -9,7 +9,7 @@ MyBatis-Flex 使用了 APTAnnotation Processing Tool技术在项目编
## 配置文件和选项
要对MyBatis-Flex 的APT细节选项进行配置你需要在`resources`目录下创建名为`mybatis-flex.properties`的文件。
要对 MyBatis-Flex 的 APT 细节选项进行配置,你需要在项目的 **根目录** 下创建名为 `apt.config` 的文件。
支持的配置选项如下:

View File

@ -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) {

View File

@ -0,0 +1,2 @@
processor.mappersGenerateEnable = false
processor.allInTables = true

View File

@ -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<Account> accounts = accountMapper.selectListByQuery(queryWrapper);
assertThat(accounts.size()).isEqualTo(1);
assertThat(accounts.get(0).getAge()).isEqualTo(18);

View File

@ -1 +0,0 @@
processor.mappersGenerateEnable = true