【发布时间】:2021-11-07 17:29:02
【问题描述】:
我在示例项目中遇到了下面这段代码。如果不正确定义 findItemsById 方法,我无法理解它是如何工作的。此方法在扩展 JpaRepositiry 的 ItemsRepository 接口中声明。
public interface ItemsRepo extends JpaRepository<Items,Long> {
Optional<Items> findItemsById(Long id);
}
在服务类中,它的定义如下所示。
public Items findItemsById(Long id) throws Throwable {
return itemsRepo.findItemsById(id).orElseThrow(()-> new UserNotFoundException("items by id "+id+" not found"));
}
如果没有 findItemsById 的实际定义,它是如何工作的。将其声明为可选是否与此有任何关系?
【问题讨论】:
-
spring 有一个引擎可以扫描任何扩展
JpaRepository并为您提供实际类的引擎。 -
@尤金。感谢您的回答。我无法理解“为您提供实际课程”。你能详细说明一下吗?再次感谢
-
-
@Eugene 再次感谢!
标签: java spring spring-boot spring-data-jpa spring-data