From f9049765f4f5f4229972b5068162402f38cb82d3 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 12 Jul 2023 10:23:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9=20gradle=20?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../processor/config/MybatisFlexConfig.java | 24 +++++++++++++++---- .../mybatisflex/processor/util/FileUtil.java | 14 +++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) 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 4b5f2686..bcc6dbd2 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 @@ -16,11 +16,17 @@ package com.mybatisflex.processor.config; +import com.mybatisflex.processor.util.FileUtil; + import javax.annotation.processing.Filer; import javax.tools.FileObject; import javax.tools.StandardLocation; -import java.io.*; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -49,8 +55,16 @@ public class MybatisFlexConfig { FileObject aptConfigFileObject = filer.getResource(StandardLocation.CLASS_OUTPUT, "", APT_FILE_NAME); List aptConfigFiles = new ArrayList<>(); + File moduleRoot = new File(aptConfigFileObject.toUri()).getParentFile().getParentFile().getParentFile(); - while (new File(moduleRoot, "pom.xml").exists()) { + + // pom.xml -> Maven 项目 + // build.gradle -> Gradle 项目 + if (new File(moduleRoot, "build.gradle").exists()) { + FileUtil.setBuildFile("build.gradle"); + } + + while (FileUtil.existsBuildFile(moduleRoot)) { File aptConfig = new File(moduleRoot, APT_FILE_NAME); if (aptConfig.exists()) { aptConfigFiles.add(aptConfig); @@ -59,7 +73,7 @@ public class MybatisFlexConfig { } for (File aptConfigFile : aptConfigFiles) { - try (InputStream stream = new FileInputStream(aptConfigFile); + try (InputStream stream = Files.newInputStream(aptConfigFile.toPath()); Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) { Properties config = new Properties(); @@ -71,7 +85,7 @@ public class MybatisFlexConfig { properties.put(key, config.getProperty((String) key)); } if ("processor.stopBubbling".equalsIgnoreCase((String) key) - && "true".equalsIgnoreCase(String.valueOf(config.getProperty((String) key)))) { + && "true".equalsIgnoreCase(String.valueOf(config.getProperty((String) key)))) { stopBubbling = true; } } @@ -90,4 +104,4 @@ public class MybatisFlexConfig { return properties.getProperty(key.getConfigKey(), key.getDefaultValue()); } -} \ No newline at end of file +} diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java index 60e6e8cf..3ed2ae24 100644 --- a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java @@ -29,6 +29,16 @@ public class FileUtil { private FileUtil() { } + private static String buildFile = "pom.xml"; + + public static void setBuildFile(String buildFile) { + FileUtil.buildFile = buildFile; + } + + public static boolean existsBuildFile(File file) { + return new File(file, buildFile).exists(); + } + public static boolean isFromTestSource(String path) { return path.contains("test-sources") || path.contains("test-annotations"); } @@ -53,7 +63,7 @@ public class FileUtil { if (file.isFile()) { return getProjectRootPath(file.getParentFile(), --count); } else { - if (new File(file, "pom.xml").exists() && !new File(file.getParentFile(), "pom.xml").exists()) { + if (new File(file, buildFile).exists() && !new File(file.getParentFile(), buildFile).exists()) { return file.getAbsolutePath(); } else { return getProjectRootPath(file.getParentFile(), --count); @@ -61,4 +71,4 @@ public class FileUtil { } } -} \ No newline at end of file +} From 12fac9a78c59063036825013042951b026c0fb05 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 12 Jul 2023 10:24:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?style:=20=E5=AE=8C=E5=96=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/RelationManyToMany.java | 60 ++++++++++--------- .../annotation/RelationManyToOne.java | 27 +++++---- .../annotation/RelationOneToMany.java | 42 +++++++------ .../annotation/RelationOneToOne.java | 26 ++++---- 4 files changed, 84 insertions(+), 71 deletions(-) diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java index 2bccd0f9..5f1c48a5 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java @@ -18,6 +18,8 @@ package com.mybatisflex.annotation; import java.lang.annotation.*; /** + * 多对多映射。 + * * @author michael */ @Inherited @@ -25,52 +27,52 @@ import java.lang.annotation.*; @Target({ElementType.FIELD}) public @interface RelationManyToMany { - /** - * 当前 entity 的属性 - * - * @return 属性名称 - */ + /** + * 当前实体类的属性。 + * + * @return 属性名称 + */ String selfField() default ""; - /** - * 目标对象的关联属性 - * - * @return 属性名称 + /** + * 目标实体类的关联属性。 + * + * @return 属性名称 */ String targetField() default ""; - /** - * 中间表 - * - * @return 中间表名称 + /** + * 中间表名称。 + * + * @return 中间表名称 */ String joinTable(); - /** - * 中间表与当前表的关联字段 - * - * @return 字段名称,列名 + /** + * 中间表与当前表的关联字段。 + * + * @return 字段名称,列名 */ String joinSelfColumn(); - - /** - * 目标表的关联字段名称 - * - * @return 字段名称和表 + /** + * 目标表的关联字段名称。 + * + * @return 字段名称和表 */ String joinTargetColumn(); - /** - * 查询排序 - * - * @return 排序方式 + /** + * 查询排序。 + * + * @return 排序方式 */ String orderBy() default ""; - - /** - * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + /** + * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + * + * @return 数据源 */ String dataSource() default ""; diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java index 584bdcd4..0bbf4aff 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java @@ -18,6 +18,8 @@ package com.mybatisflex.annotation; import java.lang.annotation.*; /** + * 多对一映射。 + * * @author michael */ @Inherited @@ -25,23 +27,24 @@ import java.lang.annotation.*; @Target({ElementType.FIELD}) public @interface RelationManyToOne { - /** - * 当前 entity 的属性 - * - * @return 属性名称 - */ + /** + * 当前实体类的属性。 + * + * @return 属性名称 + */ String selfField(); - - /** - * 目标对象的关联属性 - * - * @return 属性名称 + /** + * 目标实体类的关联属性。 + * + * @return 属性名称 */ String targetField() default ""; - /** - * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + /** + * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + * + * @return 数据源 */ String dataSource() default ""; diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java index 8c64ccff..5fcdf7f0 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToMany.java @@ -18,6 +18,8 @@ package com.mybatisflex.annotation; import java.lang.annotation.*; /** + * 一对多映射。 + * * @author michael */ @Inherited @@ -25,36 +27,38 @@ import java.lang.annotation.*; @Target({ElementType.FIELD}) public @interface RelationOneToMany { - /** - * 当前 entity 的属性 - * - * @return 属性名称 - */ + /** + * 当前实体类的属性。 + * + * @return 属性名称 + */ String selfField() default ""; - /** - * 目标对象的关联属性 - * - * @return 属性名称 + /** + * 目标实体类的关联属性。 + * + * @return 属性名称 */ String targetField(); - /** - * 查询排序 - * - * @return 排序方式 + /** + * 查询排序。 + * + * @return 排序方式 */ String orderBy() default ""; - /** - * 现在查询的数据量 - * - * @return 数据量 + /** + * 现在查询的数据量。 + * + * @return 数据量 */ int limit() default 0; - /** - * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + /** + * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + * + * @return 数据源 */ String dataSource() default ""; diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java index 93a3e28b..ca79ecf0 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationOneToOne.java @@ -18,6 +18,8 @@ package com.mybatisflex.annotation; import java.lang.annotation.*; /** + * 一对一映射。 + * * @author michael */ @Inherited @@ -25,22 +27,24 @@ import java.lang.annotation.*; @Target({ElementType.FIELD}) public @interface RelationOneToOne { - /** - * 当前 entity 的属性 - * - * @return 属性名称 - */ + /** + * 当前实体类的属性。 + * + * @return 属性名称 + */ String selfField() default ""; - /** - * 目标对象的关联属性 - * - * @return 属性名称 + /** + * 目标实体类的关联属性。 + * + * @return 属性名称 */ String targetField(); - /** - * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + /** + * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 + * + * @return 数据源 */ String dataSource() default ""; From ef130bd6b77c24da78d0960aa6b1762fc8157d48 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 12 Jul 2023 10:24:26 +0800 Subject: [PATCH 3/3] =?UTF-8?q?style:=20=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=81=E6=9C=89=E6=9E=84=E9=80=A0=E5=99=A8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/relation/RelationManager.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java index 8986609f..88766881 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java @@ -38,15 +38,18 @@ import java.util.concurrent.ConcurrentHashMap; */ public class RelationManager { - private static Map, List> classRelations = new ConcurrentHashMap<>(); + private RelationManager() { + } - private static List getRelations(Class clazz) { - return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); - } + private static Map, List> classRelations = new ConcurrentHashMap<>(); - private static List doGetRelations(Class entityClass) { - List allFields = ClassUtil.getAllFields(entityClass); - List relations = new ArrayList<>(); + private static List getRelations(Class clazz) { + return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); + } + + private static List doGetRelations(Class entityClass) { + List allFields = ClassUtil.getAllFields(entityClass); + List relations = new ArrayList<>(); for (Field field : allFields) { RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class); if (manyToManyAnnotation != null) {