这段代码的泛型写法,写出代码 public Review addProjectSpecialty(Integer reviewid, List<Integer> projectspecialtyids) { ReviewDO reviewdo = basedorepo.findById(reviewid).get(); projectspecialtyids.forEach(item -> { ProjectSpecialtyDO projectspecialtydo = projspecdorepo.findById(item).get(); reviewdo.getProjectSpecialtyDOs().add(projectspecialtydo); }); return dataconverter.toTModel(basedorepo.save(reviewdo)); }
时间: 2023-06-13 17:02:56 浏览: 84
这段代码的泛型写法已经很好了,没有必要再进行泛型改写。如果你一定要进行泛型改写,可以尝试以下写法:
```
public <T extends BaseEntity, S extends BaseEntity> T addSpecialtyToEntity(Integer entityId, List<Integer> specialtyIds, JpaRepository<T, Integer> entityRepo, JpaRepository<S, Integer> specialtyRepo, Function<T, List<S>> getSpecialtiesFunc) {
T entity = entityRepo.findById(entityId).get();
specialtyIds.forEach(specialtyId -> {
S specialty = specialtyRepo.findById(specialtyId).get();
getSpecialtiesFunc.apply(entity).add(specialty);
});
return entityRepo.save(entity);
}
```
然后可以使用以下方式调用:
```
Review review = addSpecialtyToEntity(reviewId, projectSpecialtyIds, reviewRepo, projectSpecialtyRepo, Review::getProjectSpecialtyDOs);
```
阅读全文