使用Spring Data R2DBC +Postgres實現(xiàn)增刪改查功能
在本教程中,我想向您展示如何通過帶有Spring WebFlux的Spring Data R2DBC 執(zhí)行各種Postgres CRUD操作。
R2DBC代表反應式關系數(shù)據(jù)庫連接。
像JPA(Java持久性API)一樣,R2DBC是關系數(shù)據(jù)庫的反應性驅動程序的規(guī)范。由于它是一個單獨的規(guī)范,因此請勿與JPA / Hibernate功能(如@OneToMany,@ManyToMany 等)比較。
我們將開發(fā)一個名為product-service的Spring Boot應用程序,該應用程序負責創(chuàng)建新產品/檢索所有產品/刪除或更新現(xiàn)有產品以執(zhí)行R2DBC的各種Postgres CRUD操作。
實體類
@Data
@ToString
<b>public</b> <b>class</b> Product {
@Id
<b>private</b> Integer id;
<b>private</b> String description;
<b>private</b> Double price;
}
我們不能在此處添加@Entity,因為這不是JPA。
Spring Data反應性存儲庫
Spring Data照常進行所有繁重的工作。我們需要通過擴展ReactiveCrudRepository為我們的實體類創(chuàng)建一個存儲庫。
<b>import</b> org.springframework.data.repository.reactive.ReactiveCrudRepository;
<b>import</b> org.springframework.stereotype.Repository;
@Repository
<b>public</b> <b>interface</b> ProductRepository <b>extends</b> ReactiveCrudRepository<Product, Integer> {
}
CRUD操作
讓我們創(chuàng)建一個服務類,以通過Spring Data Reactive Repository執(zhí)行Postgres CRUD操作。
@Service
<b>public</b> <b>class</b> ProductService {
@Autowired
<b>private</b> ProductRepository repository;
<b>public</b> Flux<Product> getAllProducts(){
<b>return</b> <b>this</b>.repository.findAll();
}
<b>public</b> Mono<Product> getProductById(<b>int</b> productId){
<b>return</b> <b>this</b>.repository.findById(productId);
}
<b>public</b> Mono<Product> createProduct(<b>final</b> Product product){
<b>return</b> <b>this</b>.repository.save(product);
}
<b>public</b> Mono<Product> updateProduct(<b>int</b> productId, <b>final</b> Mono<Product> productMono){
<b>return</b> <b>this</b>.repository.findById(productId)
.flatMap(p -> productMono.map(u -> {
p.setDescription(u.getDescription());
p.setPrice(u.getPrice());
<b>return</b> p;
}))
.flatMap(p -> <b>this</b>.repository.save(p));
}
<b>public</b> Mono<Void> deleteProduct(<b>final</b> <b>int</b> id){
<b>return</b> <b>this</b>.repository.deleteById(id);
}
}
REST API
現(xiàn)在是時候通過REST API公開服務了:
@RestController
@RequestMapping(<font>"product"</font><font>)
<b>public</b> <b>class</b> ProductController {
@Autowired
<b>private</b> ProductService productService;
@GetMapping(</font><font>"all"</font><font>)
<b>public</b> Flux<Product> getAll(){
<b>return</b> <b>this</b>.productService.getAllProducts();
}
@GetMapping(</font><font>"{productId}"</font><font>)
<b>public</b> Mono<ResponseEntity<Product>> getProductById(@PathVariable <b>int</b> productId){
<b>return</b> <b>this</b>.productService.getProductById(productId)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
}
@PostMapping
<b>public</b> Mono<Product> createProduct(@RequestBody Mono<Product> productMono){
<b>return</b> productMono.flatMap(<b>this</b>.productService::createProduct);
}
@PutMapping(</font><font>"{productId}"</font><font>)
<b>public</b> Mono<Product> updateProduct(@PathVariable <b>int</b> productId,
@RequestBody Mono<Product> productMono){
<b>return</b> <b>this</b>.productService.updateProduct(productId, productMono);
}
@DeleteMapping(</font><font>"/{id}"</font><font>)
<b>public</b> Mono<Void> deleteProduct(@PathVariable <b>int</b> id){
<b>return</b> <b>this</b>.productService.deleteProduct(id);
}
}
</font>
配置
Spring Data反應驅動程序需要這樣的配置才能連接到Postgres DB。
方法1:使用application.properties
spring.r2dbc.url=r2dbc:postgresql:<font><i>//localhost:5432/productdb</i></font><font> spring.r2dbc.username=vinsguru spring.r2dbc.password=admin </font>
方法2:公開連接工廠bean
@Configuration
<b>public</b> <b>class</b> R2DBCConfig {
@Bean
<b>public</b> ConnectionFactory connectionFactory() {
<b>return</b> ConnectionFactories.get(
ConnectionFactoryOptions.builder()
.option(DRIVER, <font>"postgresql"</font><font>)
.option(HOST, </font><font>"localhost"</font><font>)
.option(PORT, 5432)
.option(USER, </font><font>"vinsguru"</font><font>)
.option(PASSWORD, </font><font>"admin"</font><font>)
.option(DATABASE, </font><font>"productdb"</font><font>)
.option(MAX_SIZE, 40)
.build());
}
}
</font>
完整的源代碼在這里。
到此這篇關于使用Spring Data R2DBC +Postgres實現(xiàn)增刪改查功能的文章就介紹到這了,更多相關Spring Data R2DBC +Postgres實現(xiàn)增刪改查內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Apache POI在Java中實現(xiàn)Excel單元格的合并
在日常工作中,Excel是一個不可或缺的工具,尤其是在處理大量數(shù)據(jù)時,本文將介紹如何使用 Apache POI 庫在 Java 中實現(xiàn) Excel 單元格的合并,需要的可以了解下2025-03-03
解決springboot中配置過濾器以及可能出現(xiàn)的問題
這篇文章主要介紹了解決springboot中配置過濾器以及可能出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java如何使用multipartFile對象解析Execl
本文介紹了如何使用Spring的MultipartFile類解析Excel文件(.xls和.xlsx),包括文件上傳、數(shù)據(jù)校驗、輸入流獲取、文件解析、數(shù)據(jù)保存和異常處理的詳細步驟2025-02-02
Java RandomAccessFile 指定位置實現(xiàn)文件讀取與寫入
這篇文章主要介紹了Java RandomAccessFile 指定位置實現(xiàn)文件讀取與寫入的相關資料,需要的朋友可以參考下2017-01-01
SpringBoot不讀取bootstrap.yml/properties文件問題
這篇文章主要介紹了SpringBoot不讀取bootstrap.yml/properties文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

