From f8c75f293bec7e64978bdea778cf2ae106b96a87 Mon Sep 17 00:00:00 2001 From: Sorghum Date: Fri, 12 Jul 2024 12:52:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=BB=E9=94=AE?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E7=9A=84=E7=AD=96=E7=95=A5,=E4=BF=AE?= =?UTF-8?q?=E6=94=B9wood.plus=E4=BB=8EClassWrap=E8=8E=B7=E5=8F=96=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E4=BF=A1=E6=81=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/noear/wood/mapper/BaseEntityWrap.java | 11 +++++------ wood/src/main/java/org/noear/wood/WoodConfig.java | 6 ++++++ .../main/java/org/noear/wood/wrap/FieldWrap.java | 3 +++ .../org/noear/wood/wrap/PrimaryKeyStrategy.java | 13 +++++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 wood/src/main/java/org/noear/wood/wrap/PrimaryKeyStrategy.java diff --git a/wood.plus/src/main/java/org/noear/wood/mapper/BaseEntityWrap.java b/wood.plus/src/main/java/org/noear/wood/mapper/BaseEntityWrap.java index 9a46adb..1eb86dd 100644 --- a/wood.plus/src/main/java/org/noear/wood/mapper/BaseEntityWrap.java +++ b/wood.plus/src/main/java/org/noear/wood/mapper/BaseEntityWrap.java @@ -48,18 +48,17 @@ public class BaseEntityWrap { throw new RuntimeException("请为BaseMapper申明实体类型"); } - Table ann = entityClz.getAnnotation(Table.class); - if (ann != null) { - tableName = ann.value(); - } + ClassWrap classWrap = ClassWrap.get(entityClz); + + tableName = classWrap.tableName; if (tableName == null) { tableName = entityClz.getSimpleName(); } - for (FieldWrap f1 : ClassWrap.get(entityClz).fieldWraps) { - if (f1.field.getAnnotation(PrimaryKey.class) != null) { + for (FieldWrap f1 : classWrap.fieldWraps) { + if (f1.pk) { pkName = f1.name; break; } diff --git a/wood/src/main/java/org/noear/wood/WoodConfig.java b/wood/src/main/java/org/noear/wood/WoodConfig.java index ddc54c0..5a6cfc1 100755 --- a/wood/src/main/java/org/noear/wood/WoodConfig.java +++ b/wood/src/main/java/org/noear/wood/WoodConfig.java @@ -6,6 +6,7 @@ import org.noear.wood.ext.Act2; import org.noear.wood.ext.Fun1; import org.noear.wood.impl.IMapperAdaptorImpl; import org.noear.wood.mapper.IMapperAdaptor; +import org.noear.wood.wrap.PrimaryKeyStrategy; import org.noear.wood.wrap.NamingStrategy; import org.noear.wood.wrap.TypeConverter; @@ -36,6 +37,11 @@ public final class WoodConfig { */ public static NamingStrategy namingStrategy = new NamingStrategy(); + /** + * 非注解的字段主键策略 + */ + public static PrimaryKeyStrategy primaryKeyStrategy = new PrimaryKeyStrategy(); + /** * 字段类型转换器 */ diff --git a/wood/src/main/java/org/noear/wood/wrap/FieldWrap.java b/wood/src/main/java/org/noear/wood/wrap/FieldWrap.java index 62dd0fa..50f4579 100644 --- a/wood/src/main/java/org/noear/wood/wrap/FieldWrap.java +++ b/wood/src/main/java/org/noear/wood/wrap/FieldWrap.java @@ -14,6 +14,7 @@ import java.lang.reflect.Modifier; public class FieldWrap { public final Field field; public final String name; + public final boolean pk; public final boolean exclude; public final boolean readonly; @@ -32,6 +33,8 @@ public class FieldWrap { name = WoodConfig.namingStrategy.fieldToColumnName(clz, f1); } + pk = WoodConfig.primaryKeyStrategy.fieldIsPrimaryKey(clz, f1); + field.setAccessible(true); _getter = findGetter(clz, f1); diff --git a/wood/src/main/java/org/noear/wood/wrap/PrimaryKeyStrategy.java b/wood/src/main/java/org/noear/wood/wrap/PrimaryKeyStrategy.java new file mode 100644 index 0000000..04fd8fa --- /dev/null +++ b/wood/src/main/java/org/noear/wood/wrap/PrimaryKeyStrategy.java @@ -0,0 +1,13 @@ +package org.noear.wood.wrap; + +import org.noear.wood.annotation.PrimaryKey; + +import java.lang.reflect.Field; + +public class PrimaryKeyStrategy { + + public boolean fieldIsPrimaryKey(Class clz, Field f) { + PrimaryKey annotation = f.getAnnotation(PrimaryKey.class); + return annotation != null; + } +} -- Gitee