Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the reactive database driver specification R2DBC

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to understand the reactive database driver specification R2DBC, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Brief introduction

R2DBC is an asynchronous, non-blocking relational database connection specification. Although some NoSQL database vendors provide reactive database clients for their databases, migrating to NoSQL is not an ideal choice for most projects. This led to the birth of a general responsive relational database connection specification. MySQL, a relational database with a large user base, also has a reactive driver, but it is not official. But Spring officially included it in the dependency pool, indicating that the quality of the library is not low. So try it today and try connecting to MySQL using R2DBC.

two。 Environmental dependence

Based on Spring Boot 2.3.1 and Spring Data R2DBC, there is also a reactive Web framework Webflux, but also depends on the r2dbc-mysql library, all Maven dependencies are:

Dev.miku r2dbc-mysql org.springframework.boot spring-boot-starter-data-r2dbc org.springframework.boot spring-boot-starter-data-jdbc org.springframework.boot spring-boot-starter-webflux

The MySQL version is 5. 7 and no other versions have been tested.

3. R2DBC configuration

All R2DBC automatic configurations are under the org.springframework.boot.autoconfigure.data.r2dbc package. If you want to configure MySQL, you must configure the corresponding connection factory interface ConnectionFactory, or you can configure it through application.yml. Personally, I prefer JavaConfig.

@ BeanConnectionFactory connectionFactory () {return MySqlConnectionFactory.from (MySqlConnectionConfiguration.builder () .host ("127.0.0.1") .port (3306) .username ("root") .password ("123456") .database ("database_name") / / additional other optional parameters omitted .build () }

For more information on configuration, please see the official description of r2dbc-mysql: https://github.com/mirromutth/r2dbc-mysql

When ConnectionFactory is configured, it is injected into the DatabaseClient object. This object is non-blocking and is used to perform database reactive client calls and response stream backpressure requests. We can operate the database in a reactive manner through this interface.

4. Write a reactive interface

Let's first create a table and write some data:

Create table client_user (user_id varchar (64) not null comment 'user uniquely marked' primary key, username varchar (64) null comment 'name, phone_number varchar (64) null comment' mobile number' Gender tinyint (1) default 0 null comment'0 unknown 1 male and 2 female')

The corresponding entities are:

Package cn.felord.r2dbc.config;import lombok.Data;/** * @ author felord.cn * / @ Datapublic class ClientUser {private String userId; private String username; private String phoneNumber; private Integer gender;}

Then we write a reactive interface for Webflux:

Package cn.felord.r2dbc.config;import org.springframework.data.r2dbc.core.DatabaseClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import reactor.core.publisher.Flux;import reactor.core.publisher.Mono;import javax.annotation.Resource;/** * The type User controller. * * @ author felord.cn * @ since 17: 07 * / @ RestController@RequestMapping ("/ user") public class UserController {@ Resource private DatabaseClient databaseClient / * * query * * @ return returns the Flux sequence containing all ClientUser * / @ GetMapping ("/ get") public Flux clientUserFlux () {return databaseClient.execute ("select * from client_user") .as (ClientUser.class) .fetch (). All ();} / * * responsive writes. * * @ return Mono object contains the number of successfully updated entries * / @ GetMapping ("/ add") public Mono insert () {ClientUser clientUser = new ClientUser (); clientUser.setUserId ("34345514644"); clientUser.setUsername ("felord.cn"); clientUser.setPhoneNumber ("3456121"); clientUser.setGender (1) Return databaseClient.insert () .into (ClientUser.class) .using (clientUser) .fetch () .rowsUpdated ();}}

The desired data results can be obtained by calling the interface.

5. Summary

At first glance, R2DBC is not as difficult as you might think, but indirectly you need to understand abstract concepts such as Flux and Mono. At the same time, at present, there is no scenario if it does not work with the Webflux framework. As far as the MySQL in this article is concerned, the R2DBC driver is still community maintenance (I have to say that PgSQL does a good job).

However, what you need to see clearly is that reaction is the future. If you want to seize the future, you need to know something about it now. This reminds me of the feeling when I first came into contact with Spring Boot five years ago.

The above is how to understand the reactive database driver specification R2DBC. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 210

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report