From 69d4c62f3544936cb37a3aa534d95fc289742be7 Mon Sep 17 00:00:00 2001 From: wangyu <727842003@qq.com> Date: Tue, 30 Mar 2021 23:19:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0db=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/beans/meta/BeanProperty.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java index a3bc2c4..ba57d14 100644 --- a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java +++ b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java @@ -1,7 +1,7 @@ package com.flyfish.framework.beans.meta; -import com.flyfish.framework.annotations.*; import com.flyfish.framework.annotations.Properties; +import com.flyfish.framework.annotations.*; import com.flyfish.framework.domain.base.Qo; import com.flyfish.framework.domain.base.Vo; import com.flyfish.framework.utils.DataUtils; @@ -13,6 +13,7 @@ import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.commons.lang3.reflect.TypeUtils; import org.springframework.beans.BeanUtils; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.mongodb.core.mapping.Document; @@ -106,11 +107,11 @@ public class BeanProperty { property.prop("code", name); } else if (field.isAnnotationPresent(DBRefValue.class)) { // 添加了数据库引用注解,自动注入类型给前端使用 - property.setType(BeanPropertyType.DB_REF); DBRefValue dbRefValue = field.getAnnotation(DBRefValue.class); - Document document = AnnotationUtils.findAnnotation(dbRefValue.value(), Document.class); - if (null != document) { - property.prop("uri", document.collection()); + Optional optional = processDbRef(dbRefValue.value()); + if (optional.isPresent()) { + property.setType(BeanPropertyType.DB_REF); + property.prop("uri", optional.get()); } else { property.setType(BeanPropertyType.STRING); } @@ -131,9 +132,9 @@ public class BeanProperty { break; case DB_REF: // 当存在db-ref时,解析为动态数据源 - Document document = AnnotationUtils.findAnnotation(clazz, Document.class); - if (null != document) { - property.prop("uri", document.collection()); + Optional optional = processDbRef(clazz); + if (optional.isPresent()) { + property.prop("uri", optional.get()); } else { property.setType(BeanPropertyType.STRING); } @@ -162,6 +163,17 @@ public class BeanProperty { return ListUtils.union(parseExtras(clazz), properties); } + /** + * 解析db引用的参数 + * + * @param clazz 类 + * @return 结果 + */ + private static Optional processDbRef(Class clazz) { + return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(clazz, Document.class)) + .map(Document::value); + } + /** * 解析额外的注解 *