我经历了stackoverflows,ModelMapper,Dozer等。我无法弄清楚将Entity映射到DTO的现有逻辑。以下是实体和DTO的类:package com.entity;public class EmployeeRequest { private User user; private String userId; public EmployeeRequest(){ } //Getters and Setters Below}package com.entity;public class User { private Address address; private Name name; public User(){ } //Getters and Setters Below}package com.entity;public class Address { private int flatNo; private String city; public Address(){ } //Getters and Setters Below}package com.entity;public class Name { private String firstName; private String lastName; public Name(){ } //Getters and Setters Below}package com.dto;public class EmployeeRequestDTO { private UserDTO user; private String userId; public EmployeeRequestDTO(){ } //Getters and Setters Below}package com.dto;public class UserDTO { private AddressDTO address; private NameDTO name; public UserDTO() { } //Getters and Setters Below}package com.dto;public class AddressDTO { private int flatNo; private String city; public AddressDTO() { } //Getters and Setters Below}package com.dto;public class NameDTO { private String firstName; private String lastName; public NameDTO(){ } //Getters and Setters Below}如何将实体值映射到DTO?分配后,实体中的任何更改都不应影响DTO,反之亦然。
2 回答
万千封印
TA贡献1891条经验 获得超3个赞
下面的1行足以映射:
EmployeeRequestDTO employeeRequestDTO = new ModelMapper().map(employeeRequest, EmployeeRequestDTO.class);
添加回答
举报
0/150
提交
取消