Conversion of DTO to entity and vice-versa

前端 未结 6 1175

I am using Spring MVC architecture with JPA in my web application. Where to convert data transfer object (DTO) to JPA entity and vice-versa, manua

6条回答
  •  无人及你
    2020-12-25 12:14

    I think you are asking about where to write whole entity-->DTO conversion logic.

    Like Your entity

    class StudentEntity {
     int age ;
     String name;
    
     //getter
     //setter
    
     public StudentDTO _toConvertStudentDTO(){
        StudentDTO dto = new StudentDTO();
        //set dto values here from StudentEntity
        return dto;
     }
    
    }
    

    Your DTO Should be like

    class StudentDTO  {
     int age ;
     String name;
    
     //getter
     //setter
    
     public StudentEntity _toConvertStudentEntity(){
        StudentEntity entity = new StudentEntity();
        //set entity values here from StudentDTO
        return entity ;
     }
    
    }
    

    And Your Controller should be like

    @Controller
    class MyController {
    
        public String my(){
    
        //Call the conversion method here like
        StudentEntity entity = myDao.getStudent(1);
        StudentDTO dto = entity._toConvertStudentDTO();
    
        //As vice versa
    
        }
    
    }
    

提交回复
热议问题