mapToObject

Java map转Object例子

先定义一个DTO

// 重写toString()
public class RoleInfo {

    private  String roleName;
    private  String roleCode;
    private  Integer level;
    private Date  createTime;
    private  Boolean vaild;

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.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+"]";
    }
}

map to object 类型

public class MapToObject {

    public static Object mapToObject(Map<String, Object> map, Class cls) throws Exception {

        if (map == null || map.size() == 0) {
            return cls.newInstance();
        }

        Object t = cls.newInstance();
        Field[] fields = cls.getDeclaredFields();
        Method[] methods = cls.getDeclaredMethods();
        Map<String, Method> methodMap = new HashMap<>();

        //所有方法
        for (Method me : methods) {
            methodMap.put(me.getName().toUpperCase(), me);
        }


        Set<String> keySet = map.keySet();
        Set<String> methodSet = methodMap.keySet();

        for (Field field : fields) {
            String name = field.getName();
            //System.out.println("Field type:"+field.getType().getTypeName());

            //map中有的属性才设值
            if (keySet.contains(name)) {
                //匹配设值方法
                String attrName = "SET" + name.toUpperCase();
                if (methodSet.contains(attrName)) {

                    //对象属性类型判断
                    if (field.getType().getTypeName().compareToIgnoreCase(map.get(name).getClass().getTypeName()) != 0) {
                        throw new Exception("设值方法参数类型不匹配,attr:" + name + ",type:" + field.getType().getTypeName() + ",参数类型:" + map.get(name).getClass().getTypeName());
                    }

                    Method m = methodMap.get(attrName);
                    m.invoke(t, map.get(name));
                }

            }

        }


        return t;

    }

    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 = (RoleInfo) MapToObject.mapToObject(map, RoleInfo.class);
            System.out.println(role.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值