- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的代码中,Appointment
是一个实体,Long id
是它的主键。
在下面的代码中,当我编写 findById(Long id)
时,所需的返回类型是 Optional
。但是当我编写 findAppointmentById(Long id)
时,所需的返回类型是 Appointment
。
findById(Long id)
和 findAppointmentById(Long id)
有什么区别??
public interface AppointmentDao extends CrudRepository<Appointment, Long>{
Optional<Appointment> findById(Long id);
}
最佳答案
What is difference between findById(Long id) and findAppointmentById(Long id) ??
区别是findById(id)
已在 CrudRepository
中声明接口(interface)返回可选,并覆盖 Optional
的返回类型与一个无关的Appointment
类型在 Java 中是不允许的
But why does findAppointmentById(Long id) return Appointment ? Is it because its a derived query and since i wrote Appointment in the query ,it will return Appoinment?
不是,是因为你声明它返回Appointment
.您可以声明它返回 Optional<Appointment>
它也会起作用。你可以在 find...
之间放任何你喜欢的东西和 ...By
, Spring Data 会简单地忽略它。
关于spring-data-jpa - JPA - 为什么派生查询返回可选对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62394934/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!