W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
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
在編寫僅指定這些方法簽名的自定義存儲(chǔ)庫接口之后,將為您生成實(shí)現(xiàn),并且可以將其與存儲(chǔ)庫的自動(dòng)關(guān)聯(lián)實(shí)例一起使用。由于Cloud Datastore要求明確選擇的字段必須全部一起出現(xiàn)在組合索引中,因此find
基于名稱的查詢方法將以SELECT *
的身份運(yùn)行。
還支持刪除查詢。例如,諸如deleteByAction
或removeByAction
之類的查詢方法會(huì)刪除findByAction
找到的實(shí)體。刪除查詢是作為單獨(dú)的讀取和刪除操作而不是作為單個(gè)事務(wù)執(zhí)行的,因?yàn)槌侵付瞬樵兊淖嫦龋駝tCloud Datastore無法在事務(wù)中查詢。
結(jié)果,removeBy
和deleteBy
名稱約定查詢方法不能通過performInTransaction
或@Transactional
批注在事務(wù)內(nèi)部使用。
刪除查詢可以具有以下返回類型:
方法可以具有org.springframework.data.domain.Pageable
參數(shù)來控制分頁和排序,或者具有org.springframework.data.domain.Sort
參數(shù)來僅控制排序。有關(guān)詳細(xì)信息,
請(qǐng)參見Spring Data文檔。
要在存儲(chǔ)庫方法中返回多個(gè)項(xiàng)目,我們支持Java集合以及org.springframework.data.domain.Page
和org.springframework.data.domain.Slice
。如果方法的返回類型為org.springframework.data.domain.Page
,則返回的對(duì)象將包括當(dāng)前頁面,結(jié)果總數(shù)和頁面總數(shù)。
返回
Page
的方法執(zhí)行附加查詢以計(jì)算總頁數(shù)。另一方面,返回Slice
的方法不會(huì)執(zhí)行任何其他查詢,因此效率更高。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: