This article uses two methods to introduce Spring Boot WebFlux using R2DBC to connect to MySQL:
- Use configuration Java classes
- Use the configuration file: application.yml
Related technologies: WebFlux, R2DBC, etc.
Spring Boot WebFlux uses the R2DBC technology to connect to MySQL. First, import the following two dependencies based on the WebFlux project:
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
Copy the code
Method 1: Use a Java configuration file
Create the directory config in the Spring Boot WebFlux project and add the following two configuration Java classes.
ConnectionFactoryConfiguration. Java configuration class, which is used to configure the MySQL connection information. As follows:
package com.codergeshu.webfluxr2dbc.config;
import dev.miku.r2dbc.mysql.MySqlConnectionConfiguration;
import dev.miku.r2dbc.mysql.MySqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/ * * *@Date: 2021/2/23 also *@author: Eric
*/
@Configuration
public class ConnectionFactoryConfiguration {
@Bean
public ConnectionFactory connectionFactory(a) {
return MySqlConnectionFactory.from(MySqlConnectionConfiguration.builder()
.host("127.0.0.1") // Host address
.port(3306) / / port
.username("root") / / user name
.password("123456") / / password
.database("webflux_demo") // The name of the connected database.build()); }}Copy the code
The r2DBcConfiguration. Java configuration class is used to enable R2DBC technology to connect to MySQL. The code is as follows:
package com.codergeshu.webfluxr2dbc.config;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
/ * * *@Date: 2021/1/26 11:29
* @author: Eric
*/
@Configuration
@EnableR2dbcRepositories
public class R2dbcConfiguration extends AbstractR2dbcConfiguration {
private final ConnectionFactory connectionFactory;
public R2dbcConfiguration(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
@Override
public ConnectionFactory connectionFactory(a) {
return this.connectionFactory; }}Copy the code
This completes the R2DBC connection to MySQL using the configuration class. After that, you can use ReactiveCrudRepository for database operations.
Method 2: Use the application.yml configuration file
In addition to the above two configuration Java classes, we can also use the application.yml file to configure the R2DBC connection to MySQL as follows:
spring:
r2dbc:
url: r2dbc:mysql:/ / 127.0.0.1:3306 / webflux_demo? serverTimezone=GMT&characterEncoding=UTF-8
username: root
password: 123456
name: r2dbc
pool:
validation-query: SELECT 1
enabled: true
Copy the code
The configuration information is similar to ConnectionFactoryConfiguration. Java and R2dbcConfiguration. A combination of Java.
conclusion
Either of the above two methods can enable WebFlux projects to connect to MySQL using R2DBC. It is recommended to choose the application.yml configuration file because it is clear and easy to manage.
The author information
Hello everyone, I am CoderGeshu, a programmer who loves life. If this article is helpful, please give a like oh 👍👍👍 in addition, welcome to follow my public account of the same name: CoderGeshu, a public account dedicated to sharing programming technology knowledge!! One person can go fast, while a group of people can go far…