我对Java项目有疑问:
public class example {
public Data getData() {
List<Users> users = usersService.findByClinicId(id);
Type targetListType = new TypeToken<List<UserDTO>>() {}.getType();
List<UserDTO> usersDTO = modelMapper.map(users, targetListType);
List<Personas> profesionales = personasService.findProfesionalsByClinicId(id);
targetListType = new TypeToken<List<PersonaDTO>>() {}.getType();
List<PersonaDTO> profesionalesDTO = modelMapper.map(profesionales, targetListType);
List<Personas> auxiliares = personasService.findAuxiliarsByClinicId(id);
targetListType = new TypeToken<List<PersonaDTO>>() {}.getType();
List<PersonaDTO> auxiliaresDTO = modelMapper.map(auxiliares, targetListType);
List<Prescripciones> prescripciones = prescripcionesService.findProfesionalsByClinicId(id);
targetListType = new TypeToken<List<PrescriptionNameDTO>>() {}.getType();
List<PrescriptionNameDTO> prescripcionesDTO = modelMapper.map(prescripciones, targetListType);
profesionales = entityToDTO(PersonaDTO.class, profesionales);
...
}
private <T> List<T> entityToDTO(Class<T> clazz, List<T> list) {
Type targetListType = new TypeToken<List<clazz>>() {}.getType();
List<PersonaDTO> auxiliaresDTO = modelMapper.map(list, targetListType);
}
}
Run Code Online (Sandbox Code Playgroud)
我们的想法是getData使用该entityToDTO函数减少函数的代码.
我遇到的问题是:
entityToDTO,我想传入TypeToken第一个参数我想要的List类型,但它不接受List,其中clazz是具有类名的变量.有没有办法在List菱形运算符内部传递一个具有我想要使用的类类型的变量?
更新:想法是减少getData功能行数.
为什么不通过Type呢?
private <T> List<T> entityToDTO(Type type, List<T> list) {
return modelMapper.map(list, type);
}
Run Code Online (Sandbox Code Playgroud)
在呼叫方方面,例如:
Type typeToUse = TypeToken.getParameterized(List.class, UserDTO.class).getType();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
238 次 |
| 最近记录: |