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 9a46adb25666b96abc00524555b372548a0cce24..1eb86ddfdd7f130e31b4077dfe21587967cf0ac8 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 ddc54c0463f4898dfb14582f2dce7ab1e8871bf0..5a6cfc1cec88293236cef56270e25c7d90ad6735 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 62dd0fa6dca557a4260bed538c8c5645475a8bae..50f45795a2f1f76eb063aa3967dab760a30bd5dd 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 0000000000000000000000000000000000000000..04fd8fae7de02f8d754f40264b57a2400d4e8865 --- /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; + } +}