我正在使用 R2DBC 和 H2 创建一个快速项目,以熟悉这个新的反应性东西。制作了一个扩展 ReactiveCrudRepository 的存储库,并且一切都很好,只要我使用 DatabaseClient 发出与我的实体匹配的 CREATE TABLE 语句...
我知道 spring 数据 R2DBC 的功能不如 spring 数据 JPA(还?),但目前有没有办法从实体类生成模式?
谢谢
我有一个 User 实体和一个 Role 实体。除了 User 实体具有对应于其各自角色的 id 的 role_id 字段这一事实之外,这些字段并不重要。由于 Spring Data R2DBC 不会在实体之间建立任何形式的关系,因此我转向 DTO 方法。我对 R2DBC 和响应式编程非常陌生,我一生都无法弄清楚如何将Flux<User>我的存储库的 findAll() 方法返回到Flux<UserDto>. 我的 UserDto 类非常简单:
@Data
@RequiredArgsConstructor
public class UserDto
{
private final User user;
private final Role role;
}
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试创建的 UserMapper 类:
@Service
@RequiredArgsConstructor
public class UserMapper
{
private final RoleRepository roleRepo;
public Flux<UserDto> map(Flux<User> users)
{
//???
}
}
Run Code Online (Sandbox Code Playgroud)
如何让这个映射器将 aFlux<User>转换为Flux<UserDto>包含用户各自角色的 a ?
谢谢!
java dto reactive-programming project-reactor spring-data-r2dbc
我在搜索反应性关系数据库驱动程序时找到了R2DBC,但找不到任何Oracle DB驱动程序。是否有人提供是否提供支持的信息?
我试图品尝 R2dbc 并使用嵌入式 H2,例如:
public ConnectionFactory connectionFactory() {
//ConnectionFactory factory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
return new H2ConnectionFactory(
H2ConnectionConfiguration.builder()
//.inMemory("testdb")
.file("./testdb")
.username("user")
.password("password").build()
);
}
Run Code Online (Sandbox Code Playgroud)
我定义了一个 bean 来创建表和初始化数据。
@Bean
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
initializer.setConnectionFactory(connectionFactory);
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql")));
initializer.setDatabasePopulator(populator);
return initializer;
}
Run Code Online (Sandbox Code Playgroud)
而且我还定义了另一个组件来通过java代码设置数据。
@Component
@Slf4j
class DataInitializer {
private final DatabaseClient databaseClient;
public DataInitializer(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
}
@EventListener(value = ContextRefreshedEvent.class)
public void init() {
log.info("start data initialization …Run Code Online (Sandbox Code Playgroud) spring-boot project-reactor spring-webflux spring-data-r2dbc r2dbc
我一直在密切关注r2dbc更新,我希望使用这种方法构建我的应用程序.我用r2dbc postgres驱动程序尝试了一些小应用程序,如下所示:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.r2dbc-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如Pivotal所述,这是一个实验项目,其最终目标是最终影响ADBA.如果包含在Spring/SpringBoot的发行版中,这可能非常有用.现在的问题是,究竟是什么阻碍了Pivotal将其包含在RELEASE版本中的缺点.是否建议使用SNAPSHOT版本?
谢谢,GV
2020Enum年 8 月15 日更新:似乎在 6 月 16日添加了支持。R2DBC 提交。
H2DBC 是否支持 PostgreSQL 枚举?我检查了他们的git page但它没有提到任何关于它的内容。如果是这样,如何使用枚举(插入、选择)?
让我们说 PostgreSQL 枚举
CREATE TYPE mood AS ENUM ('UNKNOWN', 'HAPPY', 'SAD', ...);
Run Code Online (Sandbox Code Playgroud)
Java类
@Data
public class Person {
private String name;
private Mood mood;
// ...
enum Mood{ UNKNOWN, HAPPY, SAD, ...}
}
Run Code Online (Sandbox Code Playgroud)
我试过:
// insert
var person = ...;
client.insert()
.table("people")
.using(person)
.then()
.subscribe(System.out::println);
// select
var query = "SELECT * FROM people";
client.execute(query)
.as(Person.class)
.fetch().all() …Run Code Online (Sandbox Code Playgroud) 我在使用 Spring Data R2DBC DatabaseClient 将条件参数绑定到 SQL 查询时遇到困难。两个参数可以为空。由于DatabaseClient需要明确指定参数为空,因此我尝试了以下语法,但条件参数未附加到现有参数中:
public Mono<Void> createAddress(Address address) {
DatabaseClient.GenericExecuteSpec bindings = databaseClient.execute(addressesQueries.getProperty("addresses.insert"))
.bind("line1", address.getLine1())
.bind("zipCode", address.getZipCode())
.bind("city", address.getCity())
.bind("countryId", address.getCountry())
.bind("id", address.getId()); // UUID
if(address.getLine2() == null) {
bindings.bindNull("line2", String.class);
} else {
bindings.bind("line2", address.getLine2());
}
if(address.getState() == null) {
bindings.bindNull("state", String.class);
} else {
bindings.bind("state", address.getState());
}
return bindings.fetch().rowsUpdated().then();
}
Run Code Online (Sandbox Code Playgroud)
SQL查询:
INSERT INTO addresses(id,line1,line2,zip_code,city,state,country) VALUES(:id,:line1,:line2,:zipCode,:city,:state,:countryId)
Run Code Online (Sandbox Code Playgroud)
我知道我可以拆分 SQL 查询来处理带/不带空参数的情况,但如果我有多个条件参数,就会有点复杂。
你知道一种解决方案可以帮助我保留一个 SQL 查询并在 Java 代码中处理条件参数吗?
sql postgresql reactor-netty spring-webflux spring-data-r2dbc
我正在尝试执行以下操作;
但是使用org.testcontainers.containers.MySQLR2DBCDatabaseContainer
任何人都可以告诉我如何实现这一点,因为 MySQLR2DBCDatabaseContainer 似乎没有以下方法:
@Testcontainers
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationIT {
@Container
public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
.withPassword("inmemory")
.withUsername("inmemory");
@DynamicPropertySource
static void postgresqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
}
@Test
public void contextLoads() {
}
}
Run Code Online (Sandbox Code Playgroud) 我在 Spring Webflux 应用程序中使用spring-boot-starter-data-r2dbc(版本 1.1.3)模块。
我想将实体生命周期回调添加到我的持久层。
使用 Spring Data JPA 可以使用@PrePersist、@PreUpdate等注释。
有没有方便的方法可以使用 Spring Data r2dbc 实现此目的?
java spring-data project-reactor spring-webflux spring-data-r2dbc
我出现以下错误:
Exception: Error creating bean with name 'inventoryService' defined in URL [jar:file:/app.jar!/BOOT-INF/classes!/com/epi/services/inventory/items/InventoryService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemRepository': Cannot resolve reference to bean 'databaseClient' while setting bean property 'databaseClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'databaseClient' available
2019-06-18 18:38:41,409 INFO [main] org.apache.juli.logging.DirectJDKLog: Stopping service [Tomcat]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.catalina.loader.WebappClassLoaderBase (jar:file:/app.jar!/BOOT-INF/lib/tomcat-embed-core-8.5.29.jar!/) to field java.lang.Thread.threadLocals
WARNING: Please consider reporting this …Run Code Online (Sandbox Code Playgroud) r2dbc ×6
java ×5
postgresql ×3
spring-boot ×3
spring-data ×3
dto ×1
enums ×1
mysql ×1
oracle ×1
schema ×1
spring ×1
sql ×1