简体   繁体   English

如何连接到 postgres 并使用 r2dbc 执行查询

[英]How to connect to postgres and execute query using r2dbc

I'm trying to write a simple function which will connect to postgres and execute a select statement.我正在尝试编写一个简单的 function 它将连接到postgres并执行 select 语句。

PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(
        PostgresqlConnectionConfiguration.builder()
            .host("localhost")
            .port(5432)
            .database("MyDB")
            .username("username")
            .password("password").build());



DatabaseClient client = DatabaseClient.create(connectionFactory);

Flux<Map<String, Object>> result = client.execute("select * from table").fetch().all();

result.map(s -> {
  System.out.println(s);
  return null;
});

The above piece of code isn't printing anything.上面的代码没有打印任何东西。 There is no error as well.也没有错误。 I can connect to DB using the same credentials.我可以使用相同的凭据连接到数据库。 What is missing in the code to stream data from DB?数据库中 stream 数据的代码中缺少什么?

Create the configuration class which is similar to the below code to connect to the PostgreSQL database创建类似于以下代码的配置 class 以连接到PostgreSQL数据库

@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {

    @Override
    public ConnectionFactory connectionFactory() {
        return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/DATABASE_NAME");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM