繁体   English   中英

将数据从 Map 映射到 DTO/VO Object 的有效方式

[英]Efficient Way of Mapping data from Map to DTO/VO Object

我有一个带有值的 Map,我想将 map 这个值传递给 DTO

与其使用 if else 条件并映射到 DTO Object,还有什么更好的方法吗?

这是我的代码

public class Test {
    public static void main(String[] args) {
        HashMap<String , Object> hashMap = new HashMap<String , Object>();
        hashMap.put("empName", "Pavan");
        hashMap.put("deptNO", "12");
        hashMap.put("country", "IND");
        hashMap.put("age", "34");
        
        // TODO Auto-generated method stub
        Set<String> keys = hashMap.keySet();
        for(String key: keys){
            Employee emp = new Employee();
            if(key.equals("empName"))
            emp.setName(hashMap.get("empName"))
        }
    }
}

public class Employee {
    
    private String empName ;
    private String deptNO ;
    private String country ;
    private String age ;
    
    // setters and getters
}

我知道传统的做法

for(String key: keys){
  Employee emp = new Employee();
  if(key.equals("empName"))
     emp.setName(hashMap.get("empName"))
}

有没有更好的方法来做到这一点?

您可以使用 BeanUtils.populate 将您的 HashMap 密钥复制到 bean,这是一个示例:

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

public class Test {
    
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

        HashMap<String , Object> hashMap = new HashMap<String , Object>();
        hashMap.put("empName", "Pavan");
        hashMap.put("deptNO", "12");
        hashMap.put("country", "IND");
        hashMap.put("age", "34");
        EmployeeDTO employeeDTO = new EmployeeDTO();
        BeanUtils.populate(employeeDTO,hashMap);
        System.out.println(employeeDTO);

    }

}

Output

EmployeeDTO{empName='Pavan', deptNO='12', country='IND', age='34'}

在这里阅读更多:

您可以为 Employee class 使用参数化构造函数

public class Employee {
    
    private String empName;
    private String deptNO;
    private String country;
    private String age;
    
    Employee(String empName, String deptNO, String country, String age){
       this.empName = empName;
       this.deptNO = deptNO;
       this.country = country;
       this.age = age;
    }
}

您可以从 map 创建员工 object。

public class Test {
    public static void main(String[] args) {
        HashMap<String , Object> hashMap = new HashMap<String , Object>();
        hashMap.put("empName", "Pavan");
        hashMap.put("deptNO", "12");
        hashMap.put("country", "IND");
        hashMap.put("age", "34");
        
        Employee emp = new Employee(hashMap.get("empName"), hashMap.get("deptNO"), hashMap.get("country"), hashMap.get("age"));
    }
}

编辑:如果您在 map 中有很多字段,您可以在 Employee class 中使用 hashmap 来获取现有属性。

public class Employee {
    private HashMap<String, Object> hashMap;
    
    Employee(HashMap<String, Object> hashMap){
       this.hashMap = hashMap;
    }

    public Object getProperty(String propertyName){
       return hashMap.get(propertyName);
    }
}

使用MapStruct 的方法 -

定义了一个接口EmployeeMapper

@Mapper
public interface EmployeeMapper {

  EmployeeMapper MAPPER = Mappers.getMapper(EmployeeMapper.class);

  @Mapping(expression = "java(parameters.get(\"empName\"))", target = "empName")
  @Mapping(expression = "java(parameters.get(\"deptNO\"))", target = "deptNO")
  @Mapping(expression = "java(parameters.get(\"country\"))", target = "country")
  @Mapping(expression = "java(parameters.get(\"age\"))", target = "age")
  Employee map(final Map<String, String> parameters);
}

并在您的代码调用中 -

Employee employee = EmployeeMapper.MAPPER.map(hashMap);

这不像您想要的那样简洁,但它确实有效。 另一种方法是使用Qualifier ,如此所述。 Qualifier方法很冗长,它并不能完全解决您的问题,因为您仍然需要为每个字段定义映射。

MapStruct目前不完全支持从Map转换,并且有一个功能请求PR等待很长时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM