欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查功能

 更新時(shí)間:2021年03月03日 10:28:08   作者:banq  
這篇文章主要介紹了使用Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查功能,本文通過兩種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在本教程中,我想向您展示如何通過帶有Spring WebFlux的Spring Data R2DBC 執(zhí)行各種Postgres CRUD操作。

R2DBC代表反應(yīng)式關(guān)系數(shù)據(jù)庫(kù)連接。

像JPA(Java持久性API)一樣,R2DBC是關(guān)系數(shù)據(jù)庫(kù)的反應(yīng)性驅(qū)動(dòng)程序的規(guī)范。由于它是一個(gè)單獨(dú)的規(guī)范,因此請(qǐng)勿與JPA / Hibernate功能(如@OneToMany,@ManyToMany 等)比較。

我們將開發(fā)一個(gè)名為product-service的Spring Boot應(yīng)用程序,該應(yīng)用程序負(fù)責(zé)創(chuàng)建新產(chǎn)品/檢索所有產(chǎn)品/刪除或更新現(xiàn)有產(chǎn)品以執(zhí)行R2DBC的各種Postgres CRUD操作。

實(shí)體類

@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,因?yàn)檫@不是JPA。

Spring Data反應(yīng)性存儲(chǔ)庫(kù)

Spring Data照常進(jìn)行所有繁重的工作。我們需要通過擴(kuò)展ReactiveCrudRepository為我們的實(shí)體類創(chuàng)建一個(gè)存儲(chǔ)庫(kù)。

<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)建一個(gè)服務(wù)類,以通過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)在是時(shí)候通過REST API公開服務(wù)了:

@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反應(yīng)驅(qū)動(dòng)程序需要這樣的配置才能連接到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>

完整的源代碼在這里。

到此這篇關(guān)于使用Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查功能的文章就介紹到這了,更多相關(guān)Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的static關(guān)鍵字你了解多少

    Java中的static關(guān)鍵字你了解多少

    這篇文章主要為大家詳細(xì)介紹了Java中的static關(guān)鍵字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • java判斷字符串中是否包含中文并過濾中文

    java判斷字符串中是否包含中文并過濾中文

    這篇文章主要為大家詳細(xì)介紹了java判斷字符串中是否包含中文,并過濾掉中文,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 解決spring jpa中update的坑

    解決spring jpa中update的坑

    這篇文章主要介紹了spring jpa中update遇到的坑及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java編程rabbitMQ實(shí)現(xiàn)消息的收發(fā)

    Java編程rabbitMQ實(shí)現(xiàn)消息的收發(fā)

    RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完成的,可復(fù)用的企業(yè)消息系統(tǒng),本文通過實(shí)例來給大家分享通過操作rabbitMQ實(shí)現(xiàn)消息的收發(fā),感興趣的朋友可以參考下。
    2017-09-09
  • 使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類

    使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類

    這篇文章主要介紹了使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表

    帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表

    這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之鏈表 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Mybatis實(shí)現(xiàn)動(dòng)態(tài)排序方式

    Mybatis實(shí)現(xiàn)動(dòng)態(tài)排序方式

    這篇文章主要介紹了Mybatis實(shí)現(xiàn)動(dòng)態(tài)排序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Spring?Security?OAuth?Client配置加載源碼解析

    Spring?Security?OAuth?Client配置加載源碼解析

    這篇文章主要為大家介紹了Spring?Security?OAuth?Client配置加載源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 深入淺析java中finally的用法

    深入淺析java中finally的用法

    finally自己由關(guān)鍵字finally和后面的finally塊組成。這篇文章重點(diǎn)給大家介紹java中finally的用法,需要的朋友參考下吧
    2018-06-06
  • Java中&和&&的區(qū)別簡(jiǎn)單介紹

    Java中&和&&的區(qū)別簡(jiǎn)單介紹

    這篇文章主要介紹了Java中&和&&的區(qū)別,&&邏輯與||邏輯或  它們都是邏輯運(yùn)算符,& 按位與|按位或它們都是位運(yùn)算符,更多詳細(xì)內(nèi)容請(qǐng)需要的小伙伴了解下面文章內(nèi)容
    2022-01-01

最新評(píng)論