猿问

Pojo到DTO-Bean映射

我经历了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,反之亦然。


子衿沉夜
浏览 150回答 2
2回答

万千封印

下面的1行足以映射:EmployeeRequestDTO employeeRequestDTO = new ModelMapper().map(employeeRequest, EmployeeRequestDTO.class);

RISEBY

我建议为此查看一下ModelProjector。它以很小的占用空间和额外的编码完全支持您所设想的用例。
随时随地看视频慕课网APP

相关分类

Java
我要回答