我必须在dao层使用实体进行dto转换。 我正在使用ModelMapper转换“实体-> Dto”和“ Dto->实体”。
下面是我的代码:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;
@Column(name = "user_first_name", length = 45)
private String userFirstName;
@Column(name = "user_last_name", length = 45)
private String userLastName;
//SETTERS AND GETTERS
public UserDto convertToDto() {
ModelMapper modelMapper = new ModelMapper();
return modelMapper.map(this, UserDto.class);
}
}
public class UserDto {
private Long userId;
private String userFirstName;
//SETTERS AND GETTERS
public User convertToEntity() {
ModelMapper modelMapper = new ModelMapper();
return modelMapper.map(this, User.class);
}
}
这是有效的方法吗?还是有什么方法可以使用ModelMapper?