好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Spring Data R2DBC +Postgres实现增删改查功能

在本教程中,我想向您展示如何通过带有 Spring WebFlux的Spring Data R2DBC 执行各种 Postgres CRUD操作。

R2DBC代表反应式关系数据库连接。

像JPA(Java持久性API)一样,R2DBC是关系数据库的反应性驱动程序的规范。由于它是一个单独的规范,因此请勿与JPA / Hibernate功能(如@OneToMany,@ManyToMany 等)比较。

我们将开发一个名为product-service的Spring Boot应用程序,该应用程序负责创建新产品/检索所有产品/删除或更新现有产品以执行R2DBC的各种Postgres CRUD操作。

实体类

?

1

2

3

4

5

6

7

8

9

10

@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为我们的实体类创建一个存储库。

?

1

2

3

4

5

6

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

@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公开服务了:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

@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

?

1

2

3

4

spring.r2dbc.url=r2dbc:postgresql:<font><i> //localhost:5432/productdb</i></font><font>

spring.r2dbc.username=vinsguru

spring.r2dbc.password=admin

</font>

方法2:公开连接工厂bean

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@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实现增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestjdon测试数据/55817

查看更多关于使用Spring Data R2DBC +Postgres实现增删改查功能的详细内容...

  阅读:29次