笔试:Map转JavaBean的方法

导读:本篇文章讲解 笔试:Map转JavaBean的方法,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

public class Map2BeanUtil {

    /**
     * Map转换成JavaBean
     *
     * @param map Map集合
     * @param beanClass bean的Class对象
     * @param <T> bean的类型
     * @return 包含bean属性字段的Map
     */
    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws IllegalAccessException, InstantiationException {
        if (map == null || map.size() == 0){
            return beanClass.newInstance();
        }
        T instance = beanClass.newInstance();
        Field[] fields = beanClass.getDeclaredFields();
        for (Field f : fields){
            int mod = f.getModifiers();
            // final 修饰的字段无法赋值
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)){
                continue;
            }
            // 访问私有属性
            f.setAccessible(true);
            // 设置 value
            f.set(instance,map.get(f.getName()));
        }
        return instance;
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("roleName", "影像管理岗");
        map.put("roleCode", "Role-2001");
        map.put("level", 1);
        map.put("createTime", new Date());
        map.put("vaild", false);

        try {
            RoleInfo role = Map2BeanUtil.mapToObject(map, RoleInfo.class);
            System.out.println(role.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class RoleInfo {

        private static String roleName;
        private  String roleCode;
        private  final Integer level = 0;
        private  Date createTime;
        private  Boolean vaild;


        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            RoleInfo.roleName = roleName;
        }

        public String getRoleCode() {
            return roleCode;
        }

        public void setRoleCode(String roleCode) {
            this.roleCode = roleCode;
        }

        public Integer getLevel() {
            return level;
        }

        // public void setLevel(Integer level) {
        //     this.level = level;
        // }

        public Date getCreateTime() {
            return createTime;
        }

        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }

        public Boolean getVaild() {
            return vaild;
        }

        public void setVaild(Boolean vaild) {
            this.vaild = vaild;
        }

        @Override
        public String toString(){
            return "RoleInfo[roleName="+roleName +",roleCode="+roleCode+",level="+level+",createTime="+createTime+",vaild="+vaild+"]";
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69721.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!