使用Spring Data R2DBC +Postgres实现增删改查功能
在本教程中,我想向您展示如何通过带有SpringWebFlux的SpringDataR2DBC执行各种PostgresCRUD操作。
R2DBC代表反应式关系数据库连接。
像JPA(Java持久性API)一样,R2DBC是关系数据库的反应性驱动程序的规范。由于它是一个单独的规范,因此请勿与JPA/Hibernate功能(如@OneToMany,@ManyToMany等)比较。
我们将开发一个名为product-service的SpringBoot应用程序,该应用程序负责创建新产品/检索所有产品/删除或更新现有产品以执行R2DBC的各种PostgresCRUD操作。
实体类
@Data @ToString publicclassProduct{ @Id privateIntegerid; privateStringdescription; privateDoubleprice; }
我们不能在此处添加@Entity,因为这不是JPA。
SpringData反应性存储库
SpringData照常进行所有繁重的工作。我们需要通过扩展ReactiveCrudRepository为我们的实体类创建一个存储库。
importorg.springframework.data.repository.reactive.ReactiveCrudRepository; importorg.springframework.stereotype.Repository; @Repository publicinterfaceProductRepositoryextendsReactiveCrudRepository{ }
CRUD操作
让我们创建一个服务类,以通过SpringDataReactiveRepository执行PostgresCRUD操作。
@Service publicclassProductService{ @Autowired privateProductRepositoryrepository; publicFluxgetAllProducts(){ returnthis.repository.findAll(); } publicMono getProductById(intproductId){ returnthis.repository.findById(productId); } publicMono createProduct(finalProductproduct){ returnthis.repository.save(product); } publicMono updateProduct(intproductId,finalMono productMono){ returnthis.repository.findById(productId) .flatMap(p->productMono.map(u->{ p.setDescription(u.getDescription()); p.setPrice(u.getPrice()); returnp; })) .flatMap(p->this.repository.save(p)); } publicMono deleteProduct(finalintid){ returnthis.repository.deleteById(id); } }
RESTAPI
现在是时候通过RESTAPI公开服务了:
@RestController @RequestMapping("product") publicclassProductController{ @Autowired privateProductServiceproductService; @GetMapping("all") publicFluxgetAll(){ returnthis.productService.getAllProducts(); } @GetMapping( "{productId}") publicMono>getProductById(@PathVariableintproductId){ returnthis.productService.getProductById(productId) .map(ResponseEntity::ok) .defaultIfEmpty(ResponseEntity.notFound().build()); } @PostMapping publicMono "{productId}") publicMonocreateProduct(@RequestBodyMono productMono){ returnproductMono.flatMap(this.productService::createProduct); } @PutMapping( updateProduct(@PathVariableintproductId, @RequestBodyMono "/{id}") publicMonoproductMono){ returnthis.productService.updateProduct(productId,productMono); } @DeleteMapping( deleteProduct(@PathVariableintid){ returnthis.productService.deleteProduct(id); } }
配置
SpringData反应驱动程序需要这样的配置才能连接到PostgresDB。
方法1:使用application.properties
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/productdb spring.r2dbc.username=vinsguru spring.r2dbc.password=admin
方法2:公开连接工厂bean
@Configuration publicclassR2DBCConfig{ @Bean publicConnectionFactoryconnectionFactory(){ returnConnectionFactories.get( ConnectionFactoryOptions.builder() .option(DRIVER,"postgresql") .option(HOST,"localhost") .option(PORT,5432) .option(USER,"vinsguru") .option(PASSWORD,"admin") .option(DATABASE,"productdb") .option(MAX_SIZE,40) .build()); } }
完整的源代码在这里。
到此这篇关于使用SpringDataR2DBC+Postgres实现增删改查功能的文章就介绍到这了,更多相关SpringDataR2DBC+Postgres实现增删改查内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。