R2DBC配置与使用
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-pool</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.8.1.RELEASE</version>
</dependency>
配置
@Configuration
public class R2dbcConfiguration {
@Bean("rosterProperties")
@ConfigurationProperties(prefix = "easemob.roster.r2dbc")
public R2dbcProperties noticeR2dbcProperties() {
return new R2dbcProperties();
}
@Bean("rosterConnectionFactory")
public ConnectionFactory connectionFactory(@Qualifier("rosterProperties") R2dbcProperties r2dbcProperties) {
ConnectionFactory connectionFactory = ConnectionFactories.get(r2dbcProperties.getUrl());
ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder(connectionFactory)
.maxIdleTime(r2dbcProperties.getPool().getMaxIdleTime())
.maxLifeTime(r2dbcProperties.getPool().getMaxLifeTime())
.initialSize(r2dbcProperties.getPool().getInitialSize())
.maxSize(r2dbcProperties.getPool().getMaxSize())
.acquireRetry(r2dbcProperties.getPool().getAcquireRetry())
.build();
return new ConnectionPool(poolConfiguration);
}
@Bean
public DatabaseClient databaseClient(ConnectionFactory connectionFactory) {
return DatabaseClient.builder()
.connectionFactory(connectionFactory)
.build();
}
@Bean
public R2dbcEntityTemplate r2dbcEntityTemplate(
@Qualifier("rosterConnectionFactory") ConnectionFactory connectionFactory) {
return new R2dbcEntityTemplate(connectionFactory);
}
}
properties
@Data
public class R2dbcProperties {
private String url;
private Pool pool;
@Data
public static class Pool {
private Duration maxIdleTime;
private Duration maxLifeTime;
private Integer initialSize;
private Integer maxSize;
private Integer acquireRetry;
}
}
DatabaseClient 与 R2dbcEntityTemplate的区别
- DatabaseClient 是基于Fluent API的数据库交互类,提供了更灵活和定制化的查询操作,需要手动编写和管理SQL语句。
- R2dbcEntityTemplate 是基于对象映射的数据库交互类,提供了更高级和方便的数据库操作接口,通过实体类和注解来进行对象和数据库表之间的映射。
DatabaseClient 的示例代码:
@Service
public class UserService {
private final DatabaseClient databaseClient;
public UserService(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
}
public Flux<User> getAllUsers() {
return databaseClient.select()
.from(User.class)
.fetch()
.all();
}
public Mono<User> getUserById(Long id) {
return databaseClient.select()
.from(User.class)
.matching(Criteria.where("id").is(id))
.fetch()
.one();
}
public Mono<User> createUser(User user) {
return databaseClient.insert()
.into(User.class)
.using(user)
.fetch()
.one();
}
public Mono<Void> deleteUserById(Long id) {
return databaseClient.delete()
.from(User.class)
.matching(Criteria.where("id").is(id))
.fetch()
.rowsUpdated()
.then();
}
}
R2dbcEntityTemplate 示例代码
@Service
public class UserService {
private final R2dbcEntityTemplate r2dbcEntityTemplate;
public UserService(R2dbcEntityTemplate r2dbcEntityTemplate) {
this.r2dbcEntityTemplate = r2dbcEntityTemplate;
}
public Flux<User> getAllUsers() {
return r2dbcEntityTemplate.select(User.class)
.all();
}
public Mono<User> getUserById(Long id) {
return r2dbcEntityTemplate.selectOne(Query.query(Criteria.where("id").is(id)), User.class);
}
public Mono<User> createUser(User user) {
return r2dbcEntityTemplate.insert(user);
}
public Mono<Boolean> deleteUserById(Long id) {
return r2dbcEntityTemplate.delete(Query.query(Criteria.where("id").is(id)), User.class)
.map(Result::getRowsUpdated)
.map(rowsUpdated -> rowsUpdated > 0);
}
}