W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Spring Data Repositories是一種功能強大的抽象,可以節(jié)省許多樣板代碼。
例如:
public interface TraderRepository extends SpannerRepository<Trader, String> { }
Spring Data生成指定接口的有效實現(xiàn),可以方便地將其自動連接到應用程序中。
SpannerRepository
的Trader
類型參數(shù)引用基礎(chǔ)域類型。第二種類型參數(shù)String
在這種情況下是指域類型的鍵的類型。
對于具有復合主鍵的POJO,此ID類型參數(shù)可以是與所有主鍵屬性兼容的Object[]
的任何后代,Iterable
或com.google.cloud.spanner.Key
的任何后代。如果域POJO類型只有一個主鍵列,則可以使用主鍵屬性類型,也可以使用Key
類型。
例如,在屬于交易者的交易中,TradeRepository
看起來像這樣:
public interface TradeRepository extends SpannerRepository<Trade, String[]> { }
public class MyApplication { @Autowired SpannerTemplate spannerTemplate; @Autowired StudentRepository studentRepository; public void demo() { this.tradeRepository.deleteAll(); String traderId = "demo_trader"; Trade t = new Trade(); t.symbol = stock; t.action = action; t.traderId = traderId; t.price = 100.0; t.shares = 12345.6; this.spannerTemplate.insert(t); Iterable<Trade> allTrades = this.tradeRepository.findAll(); int count = this.tradeRepository.countByAction("BUY"); } }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: