示例
javax.persistence.Column.java
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String name() default "";
boolean unique() default false;
boolean nullable() default true;
boolean insertable() default true;
boolean updatable() default true;
String columnDefinition() default "";
String table() default "";
int length() default 255;
int precision() default 0;
int scale() default 0;
}
com.pxl.springbootdemo.entity.Address .java
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "phone", nullable = true, length = 11)
private String phone;
@Column(name = "zipcode", nullable = true, length = 6)
private String zipcode;
@Column(name = "address", nullable = true, length = 100)
private String addressStr;
@Column
private String addressStr1;
private String addressStr2;
public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = new HashMap<>();
static {
Field[] fields = Address.class.getDeclaredFields();
for (Field field : fields) {
Column columnAnnotation = field.getAnnotation(Column.class);
if (columnAnnotation == null) {
continue;
}
String name = columnAnnotation.name();
boolean nullable = columnAnnotation.nullable();
if (StrUtil.isEmpty(name)) {
continue;
}
COLUMN_ANNOTATION_VALUE_MAP.put(name, nullable);
}
}
优化-抽取工具方法
package com.pxl.springbootdemo.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.util.ReflectUtil;
import com.sun.tools.javac.util.Pair;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class AnnotationUtil extends cn.hutool.core.annotation.AnnotationUtil {
public static <A extends Annotation> List<Field> findAnnotatedFields(Class<?> beanClass, Class<A> annotationClass) {
Field[] annotatedFields = ReflectUtil.getFields(beanClass, field -> getAnnotation(field, annotationClass) != null);
if (annotatedFields == null) {
return Collections.emptyList();
}
return Arrays.asList(annotatedFields);
}
public static <A extends Annotation> List<A> findAnnotationsOnFields(Class<?> beanClass, Class<A> annotationClass) {
Field[] fields = ReflectUtil.getFields(beanClass);
if (fields == null) {
return Collections.emptyList();
}
return Arrays.stream(fields)
.map(field -> AnnotationUtils.getAnnotation(field, annotationClass))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public static <A extends Annotation, R> List<R> getAnnotationValuesOnFields(Class<?> beanClass, Class<A> annotationClass
, Func1<A, R> propertyName, Predicate<R> predicate) {
List<A> annotations = findAnnotationsOnFields(beanClass, annotationClass);
return CollUtil.emptyIfNull(annotations)
.stream()
.map(propertyName::callWithRuntimeException)
.filter(predicate)
.collect(Collectors.toList());
}
public static <A extends Annotation, R1, R2> Map<R1, R2> getTwoAnnotationValues(Class<?> beanClass, Class<A> annotationClass
, Func1<A, R1> keyPropertyName, Func1<A, R2> valuePropertyName, BiPredicate<R1, R2> predicate) {
List<A> annotations = findAnnotationsOnFields(beanClass, annotationClass);
return CollUtil.emptyIfNull(annotations)
.stream()
.map(e -> Pair.of(keyPropertyName.callWithRuntimeException(e), valuePropertyName.callWithRuntimeException(e)))
.filter(pair -> predicate.test(pair.fst, pair.snd))
.collect(Collectors.toMap(pair -> pair.fst, pair -> pair.snd, (k1, k2) -> k1));
}
}
public class Address {
public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = AnnotationUtil.getTwoAnnotationValues(Address.class, Column.class, Column::name, Column::nullable
, ObjUtil::isAllNotEmpty);
}