Some examples for basic usage for Spirng r2dbc.
- Spring Boot 2.3.0.RELEASE
- Spring Boot WebFlux
- Embeded jersey container
- Spring Boot Data R2DBC
- r2dbc mysql
- lombok
- jdk 8 or above
- project root
- config: configuration for Spring Boot and Spring Data
- controller: controller classes
- entity: database table mapping
- repository: derived from ReactiveCrudRepository
- service: service layer
- Runnable example for WebFlux and Reactive MySQL
- Controller-based reactive programming paradism
- support CRUD
- custom naming strategy to keep name conversion to snake_case style (@Column annotation is no need)
- Basic entity field are no need set manually, like createdAt and updatedAt
- Basic Fields
- id (set by MySQL automatically)
- remark (optional)
- active (default 1)
- createdAt (set by MySQL automatically)
- createdBy
- updatedAt (set and update by MySQL automatically)
- updatedBy
- Spring R2DBC is not support Data Audit, so annotations like @CreatedBy is not supported yet
CREATE TABLE `spring_r2dbc_samples`.`Student` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`gender` char(1) NOT NULL,
`birthday` date NOT NULL,
`address` varchar(300) NULL,
`remark` varchar(1000) NULL,
`active` tinyint NOT NULL DEFAULT 1,
`createdAt` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
`createdBy` varchar(50) NOT NULL,
`updatedAt` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
`updatedBy` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_main`(`code`)
);
return studentRepository.findByCodeAndActiveTrue(code)
.switchIfEmpty(Mono.error(new IllegalArgumentException("invalid student code: " + code)));
student.setAddress(address);
student.setRemark(remark);
return studentRepository.save(student);