java实体类型转换(利用java反射机制)

说明:

通用实体类型:Snv.java 

public class Snv {

    private String name;

    private String sex;

    private Date birthday;

    private Double account;

    private Map fvMap;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Double getAccount() {
        return account;
    }

    public void setAccount(Double account) {
        this.account = account;
    }

    public Map getFvMap() {
        return fvMap;
    }

    public void setFvMap(Map fvMap) {
        this.fvMap = fvMap;
    }
}

具体实体类型:HelpPlan.java

public class HelpPlanDto {

    private Long id;

    private String planCode;

    private String title;

    private String introduction;

    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPlanCode() {
        return planCode;
    }

    public void setPlanCode(String planCode) {
        this.planCode = planCode;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

实体类对象的字段对象:Fv.java 如下:

public class Fv {

    private String fvName;

    private String dataType;

    private Object value;

    public Fv(String fvName, String dataType, Object value) {
        this.fvName = fvName;
        this.dataType = dataType;
        this.value = value;
    }

    public String getFvName() {
        return fvName;
    }

    public void setFvName(String fvName) {
        this.fvName = fvName;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

则,现需要将 HelpPlan对象由通用Snv类型转化为HelpPlan类型,利用java的反射原理:

public class DTOUtils {

    public T snv2Dto(Snv snv,Class clazz) throws Exception{
        Map fvMap = snv.getFvMap();
        //获取字段数组
        Field[] fields = clazz.getDeclaredFields();
        //创建对象
        T ob = clazz.newInstance();
        for (Field field : fields) {
            //取消每个属性的安全检查 ,否則无法获取private字段值
            field.setAccessible(true);
            //获取字段的属性
            String fieldTypeName = field.getGenericType().toString();
            Class fieldType = getFieldType(fieldTypeName);
            //获取属性名称
            String name = field.getName();
            String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1,name.length());
            Fv fv = fvMap.get(name);
            if(fv!=null){
                fv.getDataType();
                //设置字段值
                Method methodSet = ob.getClass().getMethod(methodName, fieldType);
                methodSet.invoke(ob,fv.getValue());
            }
        }
        return ob;
    }

    public Snv dto2Snv(Object object,Class clazz){
        return null;
    }

    /**
     * 返回字段类型
     * @author
     * @param fieldTypeName
     * @return
     */
    public static Class getFieldType(String fieldTypeName){
        Class fieldType = null;
        if(fieldTypeName.equals("class java.lang.String"))
            fieldType = String.class;
        else if(fieldTypeName.equals("class java.lang.Integer"))
            fieldType = Integer.class;
        else if(fieldTypeName.equals("class java.lang.Boolean"))
            fieldType = Boolean.class;
        else if(fieldTypeName.equals("class java.util.Date"))
            fieldType = java.util.Date.class;
        else if(fieldTypeName.equals("class java.lang.Double"))
            fieldType = Double.class;
        else if(fieldTypeName.equals("class java.lang.Long"))
            fieldType = Long.class;
        else
            fieldType = Object.class;
        return fieldType;
    }

}

进行测试:

/**
         * 初始化实验类
         */
        Snv snv = new Snv();
        Fv id = new Fv("id","01",2L);
        Fv planCode = new Fv("planCode","02","01001");
        Fv title = new Fv("title","02","title");
        Fv introduction = new Fv("introduction","02","introduction");
        Fv description = new Fv("description","02","description");
        Map fvMap = new HashMap<>();
        fvMap.put("id",id);
        fvMap.put("planCode",planCode);
        fvMap.put("title",title);
        fvMap.put("introduction",introduction);
        fvMap.put("description",description);
        snv.setFvMap(fvMap);


        HelpPlanDto helpPlan = null;

        //使用工具类进行类型转化:
        DTOUtils dtoUtils = new DTOUtils<>();
        try {
            helpPlan = dtoUtils.snv2Dto(snv,HelpPlanDto.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

完成。

你可能感兴趣的:(Java基础)