注解:columnHead
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ColumnHead {
String key() default "";
String title() default "";
boolean show() default true;
PropertyType type() default PropertyType.TEXT;
}
注解处理器 ColumnHeadResolver
package com.piowind.wic.common.annotation;
import com.piowind.wic.common.pojo.PaginationVO;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
/**
* @Author: alison
* @Date: 2019/6/26
* @Version: 0.1
**/
@UtilityClass
public class ColumnHeadResolver {
public static List<PaginationVO.Head> getHeads(Class<?> clazz) {
return getHeads(clazz, "");
}
public static List<PaginationVO.Head> getHeads(Class<?> clazz, String condition) {
List<PaginationVO.Head> heads = new ArrayList<>();
try {
HashMap<String, Object> conditionMap = new HashMap<>();
if (StringUtils.isNoneBlank(condition)) {
String[] arrStr = condition.split(",");
Arrays.stream(arrStr).forEach(item -> {
int index = item.indexOf(":");
conditionMap.put(item.substring(0, index).trim(), item.substring(index + 1).trim());
});
}
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
ColumnHead head = f.getDeclaredAnnotation(ColumnHead.class);
PaginationVO.Head h = new PaginationVO.Head();
h.setKey(head.key());
if (StringUtils.isBlank(head.key())) {
h.setKey(f.getName());
}
h.setTitle(head.title());
h.setType(head.type());
h.setShow(head.show());
if (StringUtils.isNoneBlank(condition) && conditionMap.get(f.getName()) != null) {
String show = (String) conditionMap.get(f.getName());
if ("1".equalsIgnoreCase(show)) {
h.setShow(true);
} else {
h.setShow(false);
}
}
heads.add(h);
}
} catch (Exception e) {
e.printStackTrace();
}
return heads;
}
}
主要是利用注解可以动态设置字段的中文名,显示与否, 字段类型等相关属性
PS: 若你觉得可以、还行、过得去、甚至不太差的话,可以“关注”一下,就此谢过!
网友评论