英文:
How to set up database connection in r2dbc?
问题
以下是要翻译的内容:
如何在r2dbc Spring Boot项目中设置PostgreSQL数据库连接?
我已经尝试了下面的配置,它连接到数据库,但没有返回任何值。
@Configuration
@EnableR2dbcRepositories
public class DatabaseConfig extends AbstractR2dbcConfiguration {
@Override
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get("r2dbc:postgresql://localhost:5432/sample");
}
}
application.properties
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
private Long id;
private String name;
}
Repository
public interface TestRepository extends ReactiveCrudRepository<Test, Long> {
}
REST控制器
@GetMapping("/test")
public Mono<Test> test() {
testRepository.findById(3L).subscribe(v -> System.out.println("Value: " + v.toString()));
return testRepository.findById(3L);
}
它在控制台打印输出,但在JSON中我只得到空的大括号{}
。
正确的配置方式是什么?是否需要其他配置?
英文:
How to set up a PostgreSQL database connection in r2dbc Spring boot project?
I have tried the below configuration, it connects to the database but it's not returning any values
@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()););
}*/
}
application.properties
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/sample
spring.r2dbc.username=postgres
spring.r2dbc.password=thirumal
spring.r2dbc.pool.enabled=true
Model
@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;
}
Repository
public interface TestRepository extends ReactiveCrudRepository<Test, Long> {
}
REST CONTROLLER:
@GetMapping("/test")
public Mono<Test> test() {
testRepository.findById(3L).subscribe(v->System.out.println("Value: " + v.toString()));
return testRepository.findById(3L);
}
It prints the output in the console but in the JSON, I get only empty braces {}
What is the correct way to configure? Any other configuration is required?
答案1
得分: 3
我找到了问题。它是Lombok
库,我没有在eclipse中安装它。
当我手动创建getter
和setter
方法时,它起作用了。
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
然后,我设置了Lombok
并使用了@Getter
和@Setter
,它就起作用了。
英文:
I found the problem. It's Lombok
library, I didn't install it in eclipse.
When I created the getter
and setter
method manually it worked.
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Then, I set up the lombok
and used @getter
and @setter
and it worked.
答案2
得分: 2
这个配置对我有用,但我使用DatabaseClient
而不是R2dbcRepositories
来查询数据:
@Configuration
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
@Override
@Bean
public ConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("localhost")
.port(5432)
.username("username")
.password("password")
.database("mydb")
.build());
}
}
然后在仓库中:
@Repository
public class MyRepository {
@Autowired
private DatabaseClient client;
public Flux<String> getString() {
....
}
}
更新:
如果连接到数据库,可能你的配置是正确的,你可以分享一下用于获取数据的代码吗?
可能你得到的结果是Mono
或者Flux
,但没有读取它(尝试使用subscribe()
)。
Mono<String> mono = db.getData();
mono.subscribe(value -> System.out.println(value));
英文:
This configuration works for me, but I use the DatabaseClient
instead of the R2dbcRepositories
to query the data:
@Configuration
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
@Override
@Bean
public ConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host("localhost")
.port(5432)
.username("username")
.password("password")
.database("mydb")
.build());
}
}
Then in the repository:
@Repository
public class MyRepository {
@Autowired
private DatabaseClient client;
public Flux<String> getString() {
....
}
}
UPDATE:
If it's connect to the database probably your configuration is right, can you share also the code used to get the data?
It's possible that you are getting the result as Mono
or Flux
, but not reading from it (try with subscribe()
).
Mono<String> mono = db.getData();
mono.subscribe(value -> System.out.println(value));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论