![](/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.