![](/img/trans.png)
[英]What is the difference between findById() and find<classname>ById()?
[英]What is the difference between findById() and existsById() in Spring Data 2.0?
Spring Data 2.0 JpaRepository: findById()
和existsById()
的区别
findById()
和existsById()
方法?根据文件 :
existById(ID id)-返回布尔值
返回具有给定id的实体是否存在。
findById(ID id)-返回可选的(对象)
通过其ID检索实体。
如此简单findById()
返回您要搜索的对象, existsById()
返回true / false,无论存储库中是否存在实体。
findById(...) 和 getOne(...) 比 existsById(...) 快得多,不清楚为什么要发明 existsById(...)
我已经对它们进行了几次测试,在我看来,性能会不断变化,但总的来说,existById 对我来说似乎要快一点。 考虑到 findById 需要创建 java object 在我们检查存在时返回existById似乎是更好的方法。
对于 spring 数据 redis 实现,看起来existsById
只是调用findById
public boolean existsById(ID id) {
return this.findById(id).isPresent();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.