Rom*_*hok 10 java spring spring-mvc spring-boot
我的 CountryServiceImpl 有问题,当我想在 CountryServiceImpl 中实现 findOne 方法时,它告诉我“类型参数 'S' 的推断类型 'S' 不在其范围内;应该扩展 'ua.com.store.entity.Country” .
我想自己修复,但我不明白这是什么意思。你能帮我解决这个问题吗?
谢谢你。
@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String countryName;
@OneToMany(mappedBy = "country")
private Set<Brand> brands = new HashSet<Brand>();
}
public interface CountryDAO extends JpaRepository<Country, Integer> {
@Query("from Country c where c.countryName=:name")
Country findByCountryName(@Param("name") String name);
}
public interface CountryService {
void save(Country country);
void delete(Country country);
List<Country> findAll();
Country findOne(int id);
Country findByCountryName(String name);
}
@Service
public class CountryServiceImpl implements CountryService {
@Autowired
private CountryDAO dao;
@Override
public void save(Country country) {
dao.save(country);
}
@Override
public void delete(Country country) {
dao.delete(country);
}
@Override
public List<Country> findAll() {
return dao.findAll();
}
@Override
public Country findOne(int id) {
return dao.findOne(id);
}
@Override
public Country findByCountryName(String name) {
return dao.findByCountryName(name);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 10
Spring 文档定义了 getOne 方法如下
<S extends T> Optional<S> findOne(Example<S> example)
Run Code Online (Sandbox Code Playgroud)
在您的方法中,您的输入参数是 int 类型的“id”,但不受接口示例的限制。
要查找带有“id”的实体,您可以使用该方法
Optional<T> findById(ID id)
Run Code Online (Sandbox Code Playgroud)
根据您的实现,您可以编写它
@Override
public Country findOne(int id) {
return dao.findById(id);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
100% 有效的解决方案如下:
@Override
public Country findOne(int id) {
return dao.findById(id).orElse(null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28841 次 |
| 最近记录: |