mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 webflux mysql + 事务
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
<version>0.1.0.M2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- jasync 的 r2dbc-mysql 驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.github.jasync-sql</groupId>
|
||||
<artifactId>jasync-r2dbc-mysql</artifactId>
|
||||
|
||||
@@ -6,13 +6,15 @@ import io.r2dbc.spi.ConnectionFactory;
|
||||
import org.springframework.boot.autoconfigure.r2dbc.R2dbcProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.r2dbc.connectionfactory.R2dbcTransactionManager;
|
||||
import org.springframework.transaction.ReactiveTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableTransactionManagement // 开启事务的支持
|
||||
public class DatabaseConfiguration {
|
||||
|
||||
@Bean
|
||||
@@ -29,4 +31,9 @@ public class DatabaseConfiguration {
|
||||
return new JasyncConnectionFactory(new MySQLConnectionFactory(configuration));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveTransactionManager transactionManager(R2dbcProperties properties) throws URISyntaxException {
|
||||
return new R2dbcTransactionManager(this.connectionFactory(properties));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
spring:
|
||||
# R2DBC 配置,对应 R2dbcProperties 配置类
|
||||
r2dbc:
|
||||
url: mysql://47.112.193.81:3306/lab-27-webflux-r2dbc
|
||||
username: lab-27-webflux-r2dbc
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE `users` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
|
||||
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `idx_username` (`username`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||||
@@ -1,4 +1,5 @@
|
||||
spring:
|
||||
# R2DBC 配置,对应 R2dbcProperties 配置类
|
||||
r2dbc:
|
||||
url: mysql://47.112.193.81:3306/lab-27-webflux-r2dbc
|
||||
username: lab-27-webflux-r2dbc
|
||||
|
||||
Reference in New Issue
Block a user