示例
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;@Columnprivate 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.annotation.AnnotationUtil;
import cn.hutool.core.lang.func.Func1;
import org.springframework.core.annotation.AnnotationUtils;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
public class MyAnnotationUtil extends AnnotationUtil {public static List<Field> findAnnotatedFields(Class<?> beanClass, Class<? extends Annotation> annotationClass) {List<Field> result = new ArrayList<>();Field[] fields = beanClass.getDeclaredFields();for (Field field : fields) {Annotation annotation = getAnnotation(field, annotationClass);if (annotation != null) {result.add(field);}}return result;}public static <A extends Annotation> List<A> findAnnotations(Class<?> beanClass, Class<A> annotationClass) {List<A> result = new ArrayList<>();Field[] fields = beanClass.getDeclaredFields();for (Field field : fields) {A annotation = AnnotationUtils.getAnnotation(field, annotationClass);if (annotation != null) {result.add(annotation);}}return result;}public static <A extends Annotation, R> List<R> getAnnotationValues(Class<?> beanClass, Class<A> annotationClass, Func1<A, R> propertyName, Predicate<R> predicate) {List<R> result = new ArrayList<>();List<A> annotations = findAnnotations(beanClass, annotationClass);for (final A annotation : annotations) {R called = propertyName.callWithRuntimeException(annotation);if (predicate.test(called)) {result.add(called);}}return result;}public static <A extends Annotation, R1, R2> Map<R1, R2> getAnnotationValueMap(Class<?> beanClass, Class<A> annotationClass, Func1<A, R1> keyPropertyName, Func1<A, R2> valuePropertyName, BiPredicate<R1, R2> predicate) {List<A> annotations = findAnnotations(beanClass, annotationClass);Map<R1, R2> result = new HashMap<>();for (final A annotation : annotations) {R1 key = keyPropertyName.callWithRuntimeException(annotation);R2 value = valuePropertyName.callWithRuntimeException(annotation);if (predicate.test(key, value)) {result.put(key, value);}}return result;}
}
public class Address {public static final Map<String, Boolean> COLUMN_ANNOTATION_VALUE_MAP = MyAnnotationUtil.getAnnotationValueMap(Address.class, Column.class, Column::name, Column::nullable, ObjUtil::isAllNotEmpty);
}