如何在 r2dbc Spring boot 项目中建立 PostgreSQL 数据库连接?
我尝试了以下配置,它连接到数据库,但没有返回任何值
@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {
@Override
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/sample");
}
/*@Override
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get(new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host("localhost")
.port(5432)
.username("postgres")
.password("thirumal")
.database("sample")
.build()););
}*/
}
应用程序属性
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/sample
spring.r2dbc.username=postgres
spring.r2dbc.password=thirumal
spring.r2dbc.pool.enabled=true
模型
@Data@NoArgsConstructor@AllArgsConstructor@Getter@Setter
@ToString
@Table("public.test")
public class Test implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4205798689305488147L;
@Id//@Column("id")
private Long id;
private String name;
}
存储库
public interface TestRepository extends ReactiveCrudRepository<Test, Long> {
}
休息控制器:
@GetMapping("/test")
public Mono<Test> test() {
testRepository.findById(3L).subscribe(v->System.out.println("Value: " + v.toString()));
return testRepository.findById(3L);
}
它在控制台中打印输出,但在 JSON 中,我只得到空括号{}
正确的配置方法是什么?还需要什么其他配置吗?