国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

使用Spring Data R2DBC +Postgres實現增刪改查功能

瀏覽:7日期:2023-07-21 09:37:31

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

R2DBC代表反應式關系數據庫連接。

像JPA(Java持久性API)一樣,R2DBC是關系數據庫的反應性驅動程序的規范。由于它是一個單獨的規范,因此請勿與JPA / Hibernate功能(如@OneToMany,@ManyToMany 等)比較。

我們將開發一個名為product-service的Spring Boot應用程序,該應用程序負責創建新產品/檢索所有產品/刪除或更新現有產品以執行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為我們的實體類創建一個存儲庫。

<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操作

讓我們創建一個服務類,以通過Spring Data Reactive Repository執行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

現在是時候通過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=vinsguruspring.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實現增刪改查功能的文章就介紹到這了,更多相關Spring Data R2DBC +Postgres實現增刪改查內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: www成人国产在线观看网站 | 久久性精品 | 毛片大全在线观看 | 国产日本三级欧美三级妇三级四 | 午夜在线观看cao | 久久一级视频 | 一区二区三区 日韩 | 在线免费观看国产 | 永久网站色视频在线观看免费 | 男女男精品视频在线播放 | 亚洲撸 | 成熟女人免费一级毛片 | 精品久久免费观看 | 一级黄色毛片播放 | 经典香港a毛片免费观看 | 一级黄免费 | 美女黄色在线网站大全 | 久久99久久精品久久久久久 | 日本久久久久久久 | 日本视频在线免费观看 | 欧美一级xxxx俄罗斯一级 | 亚洲国产欧美精品一区二区三区 | 黄色成人免费观看 | 久久久久久久久久免免费精品 | 久久久精品一区二区三区 | 国产成人久久精品二区三区牛 | 日韩第一视频 | 国产欧美日韩不卡在线播放在线 | 中国一级毛片特级毛片 | 国产成人精品日本亚洲网站 | 草草草在线观看 | 欧美乱大交xxxxx在线观看 | a级毛片免费观看在线播放 a级毛片免费看 | 国内精品2020情侣视频 | 免费欧美在线视频 | 日韩欧美一区二区三区不卡在线 | 中文字幕日韩欧美一区二区三区 | 亚洲国产精品激情在线观看 | 国产中文字幕在线播放 | 99re5久久在热线播放 | 宅女福利视频在线看免费网站 |