feat: 增加db引用注解

This commit is contained in:
wangyu 2021-03-30 23:19:18 +08:00
parent b5b8611b6c
commit 69d4c62f35
1 changed files with 20 additions and 8 deletions

View File

@ -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<String> 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<String> 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<String> processDbRef(Class<?> clazz) {
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(clazz, Document.class))
.map(Document::value);
}
/**
* 解析额外的注解
*