W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
public interface TradeRepository extends DatastoreRepository<Trade, String[]> { List<Trader> findByAction(String action); int countByAction(String action); boolean existsByAction(String action); List<Trade> findTop3ByActionAndSymbolAndPriceGreaterThanAndPriceLessThanOrEqualOrderBySymbolDesc( String action, String symbol, double priceFloor, double priceCeiling); Page<TestEntity> findByAction(String action, Pageable pageable); Slice<TestEntity> findBySymbol(String symbol, Pageable pageable); List<TestEntity> findBySymbol(String symbol, Sort sort); }
在上面的示例中,TradeRepository
中的查詢方法是使用https://docs.spring.io/spring-data/data-commons/docs/current/reference/html#repositories基于方法名稱生成的。 query-methods.query-creation [Spring Data查詢創(chuàng)建命名約定]。
Cloud Datastore僅支持通過AND連接的過濾器組件以及以下操作:
equals
greater than or equals
greater than
less than or equals
less than
is null
在編寫僅指定這些方法簽名的自定義存儲庫接口之后,將為您生成實現(xiàn),并且可以將其與存儲庫的自動關(guān)聯(lián)實例一起使用。由于Cloud Datastore要求明確選擇的字段必須全部一起出現(xiàn)在組合索引中,因此find
基于名稱的查詢方法將以SELECT *
的身份運行。
還支持刪除查詢。例如,諸如deleteByAction
或removeByAction
之類的查詢方法會刪除findByAction
找到的實體。刪除查詢是作為單獨的讀取和刪除操作而不是作為單個事務(wù)執(zhí)行的,因為除非指定了查詢的祖先,否則Cloud Datastore無法在事務(wù)中查詢。
結(jié)果,removeBy
和deleteBy
名稱約定查詢方法不能通過performInTransaction
或@Transactional
批注在事務(wù)內(nèi)部使用。
刪除查詢可以具有以下返回類型:
方法可以具有org.springframework.data.domain.Pageable
參數(shù)來控制分頁和排序,或者具有org.springframework.data.domain.Sort
參數(shù)來僅控制排序。有關(guān)詳細信息,
請參見Spring Data文檔。
要在存儲庫方法中返回多個項目,我們支持Java集合以及org.springframework.data.domain.Page
和org.springframework.data.domain.Slice
。如果方法的返回類型為org.springframework.data.domain.Page
,則返回的對象將包括當(dāng)前頁面,結(jié)果總數(shù)和頁面總數(shù)。
返回
Page
的方法執(zhí)行附加查詢以計算總頁數(shù)。另一方面,返回Slice
的方法不會執(zhí)行任何其他查詢,因此效率更高。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: