MVC架构和JPA.
在哪里手动将DTO转换为实体,反之亦然(不使用任何框架)?
我想你在询问在哪里写整个实体 – > DTO转换逻辑.
喜欢你的实体
class StudentEntity {
int age ;
String name;
//getter
//setter
public StudentDTO _toConvertStudentDTO(){
StudentDTO dto = new StudentDTO();
//set dto values here from StudentEntity
return dto;
}
}
你的DTO应该是这样的
class StudentDTO {
int age ;
String name;
//getter
//setter
public StudentEntity _toConvertStudentEntity(){
StudentEntity entity = new StudentEntity();
//set entity values here from StudentDTO
return entity ;
}
}
你的控制器应该是这样的
@Controller
class MyController {
public String my(){
//Call the conversion method here like
StudentEntity entity = myDao.getStudent(1);
StudentDTO dto = entity._toConvertStudentDTO();
//As vice versa
}
}
翻译自:https://stackoverflow.com/questions/28703401/conversion-of-dto-to-entity-and-vice-versa