本文主要是介绍Java - 反射设置对象字段字典值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需求:查询数据时部分字段需要根据字典表中的编码进行转换获取对应的中文。
自定义注解
标注需要字典填充的字段,设置字段对应的字典编码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 需要字典填充的变更标注* DictFieldTag 标注需要字典填充的字段,dictCode值记录字段对应的字典编码*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface DictFieldTag {/*** 对应需记录变更字段** @return*/String dictCode() default "";}
实体类
为四个字段分别设置了不同的字典值标识
/*** 导出报表数据对象* */
@Data
public class UserDataVO implements Serializable {@ApiModelProperty(value = "用户id")private Long userId;@ApiModelProperty(value = "用户名称")private String userName;// 用户类型:0普通用户/1管理员用户@DictFieldTag(dictCode = "user_type")@ApiModelProperty(value = "用户类型")private String userType;// 用户性别:MALE男性/FEMALE女性@DictFieldTag(dictCode = "gender")@ApiModelProperty(value = "用户性别")private String gender;// 用户状态:E正常/F冻结@DictFieldTag(dictCode = "user_status")@ApiModelProperty(value = "用户状态")private String userStatus;
}
反射工具类
import cn.hicloudnet.dc.dcims.mict.utils.StringUtils;
import cn.hicloudnet.dc.dcims.mict.utils.objectReflex.annotation.DictFieldTag;
import cn.hutool.core.collection.CollectionUtil;
import com.beust.jcommander.Strings;
import java.lang.reflect.Field;
import java.util.*;/*** 对象反射设置字段字典值工具类*/
public class ReflectionDictUtils {/*** 通过反射设置对象字典值** @param obj 目标对象*/public static void setDictName(Object obj, Map<String, Map<String, String>> dictCodeMap) throws IllegalAccessException, NoSuchFieldException {Class<?> clazz = obj.getClass();Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {// 设置访问权限,因为有些字段可能为私有field.setAccessible(true);// 获取字段值Object fieldValue = field.get(obj);// 通过自定义注释过滤:无字典注释的不往下走DictFieldTag dictField = field.getAnnotation(DictFieldTag.class);if (dictField == null) {continue;}Map<String, String> codeValueMap = dictCodeMap.get(dictField.dictCode());if (codeValueMap != null) {String fieldValueStr = String.valueOf(fieldValue);if (Objects.nonNull(fieldValue) && StringUtils.isNotBlank(fieldValueStr)) {// 兼容多选的值String dictValueStr = "";List<String> dictValueList = new ArrayList<>();String[] valueSplit = fieldValueStr.split(",");for (String fieldVa : valueSplit) {String dictValue = codeValueMap.getOrDefault(fieldVa, "");dictValueList.add(dictValue);}if (CollectionUtil.isNotEmpty(dictValueList)) {dictValueStr = Strings.join(',', dictValueList);}field.set(obj, dictValueStr);} else {field.set(obj, "");}}}}public static void main(String[] args) throws Exception {List<UserDataVO> userList = new ArrayList<>();UserDataVO user1 = new UserDataVO();user1.setUserId(1L);user1.setUserName("小李");user1.setUserType("0");user1.setGender("MALE");user1.setUserStatus("E");userList.add(user1);UserDataVO user2 = new UserDataVO();user2.setUserId(3L);user2.setUserName("小陈");user2.setUserType("1");user2.setGender("FEMALE");user2.setUserStatus("F");userList.add(user1);// 字典值MapMap<String, Map<String, String>> dictCodeMap = new HashMap<>();Map<String, String> userTypeMap = new HashMap<>();userTypeMap.put("0", "普通用户");userTypeMap.put("1", "管理员用户");dictCodeMap.put("user_type", userTypeMap);Map<String, String> genderMap = new HashMap<>();genderMap.put("MALE", "男");genderMap.put("FEMALE", "女");dictCodeMap.put("gender", genderMap);Map<String, String> userStatusMap = new HashMap<>();userStatusMap.put("E", "正常");userStatusMap.put("F", "冻结");dictCodeMap.put("user_status", userStatusMap);// 反射设置字典值getAllFieldsValues(userList, dictCodeMap);userList.forEach(u ->{System.out.println(u);});}
}
输出:
{userStatus=正常, gender=男, userType=普通员工, userName=小李, userId=1}
{userStatus=冻结, gender=女, userType=管理员用户, userName=小陈, userId=2}
这篇关于Java - 反射设置对象字段字典值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!