java—将数据从mapMap到dto/vo对象的有效方法

x4shl7ld  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(1367)

我有一个带有值的Map,我想把这个值Map到dto
除了使用if-else条件和Map到dto对象之外,还有更好的方法吗
这是我的密码

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"))
}

有没有更好的办法?

hfyxw5xn

hfyxw5xn1#

可以为employee类设置参数化构造函数

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创建employee对象。

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类中的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);
    }
}
ryhaxcpt

ryhaxcpt2#

您可以使用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);

    }

}

输出

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

阅读更多信息:
https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/beanutils.html
https://www.tutorialspoint.com/java_beanutils/data_type_conversions_beanutils_and_convertutils.htm

gg0vcinb

gg0vcinb3#

使用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` 这种方法是冗长的,它不能完全解决您的问题,因为您仍然需要为每个字段定义Map。 `MapStruct` 当前不完全支持从 `Map` 还有一个特性请求和pr等待了很长时间。

相关问题