这是一个用于映射的映射器接口:
@Mapper
public interface DoctorMapper {
DoctorMapper INSTANCE = Mappers.getMapper(DoctorMapper.class);
DoctorDto toDto(Doctor doctor);
}
Run Code Online (Sandbox Code Playgroud)
这是一个模型类:
public class Doctor {
private int id;
private String name;
// getter and setter and constructors
}
Run Code Online (Sandbox Code Playgroud)
这是一个模型类:
public class DoctorDto {
private int id;
private String name;
// getter and setter and constructors
}
Run Code Online (Sandbox Code Playgroud)
控制器类:
@RestController
public class SimpleController {
@RequestMapping("/")
String hello() {
Doctor d= new Doctor(1,"Hari");
DoctorDto doctorDto = DoctorMapper.INSTANCE.toDto(d);
System.out.println(doctorDto.toString());
return "Hello World, Spring Boot!";
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议将 doctor 实体映射到 doctordto …