Spring Cloud Datastore Repositories

2024-01-11 16:01 更新

Spring Data Repositories是可以減少樣板代碼的抽象。

例如:

public interface TraderRepository extends DatastoreRepository<Trader, String> {
}

Spring Data生成指定接口的有效實(shí)現(xiàn),可以將其自動連接到應(yīng)用程序中。

DatastoreRepositoryTrader類型參數(shù)是指基礎(chǔ)域類型。在這種情況下,第二個(gè)類型參數(shù)String是指域類型的鍵的類型。

public class MyApplication {

	@Autowired
	TraderRepository traderRepository;

	public void demo() {

		this.traderRepository.deleteAll();
		String traderId = "demo_trader";
		Trader t = new Trader();
		t.traderId = traderId;
		this.tradeRepository.save(t);

		Iterable<Trader> allTraders = this.traderRepository.findAll();

		int count = this.traderRepository.count();
	}
}

Repositories允許您定義自定義查詢方法(在以下各節(jié)中詳細(xì)介紹),以基于過濾和分頁參數(shù)來檢索,計(jì)數(shù)和刪除。過濾參數(shù)可以是您配置的自定義轉(zhuǎn)換器支持的類型。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號