如何在R2DBC中设置数据库连接?

huangapple go评论69阅读模式
英文:

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(&quot;r2dbc:postgresql://localhost:5432/sample&quot;);
	}

	/*@Override
	public ConnectionFactory connectionFactory() {
		return ConnectionFactories.get(new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration.builder()
                .host(&quot;localhost&quot;)
                .port(5432)
                .username(&quot;postgres&quot;)
                .password(&quot;thirumal&quot;)
                .database(&quot;sample&quot;)
                .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(&quot;public.test&quot;)
public class Test implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4205798689305488147L;

	@Id//@Column(&quot;id&quot;)
	private Long id;
	
	private String name;
	
}

Repository

public interface TestRepository extends ReactiveCrudRepository&lt;Test, Long&gt; {

}

REST CONTROLLER:

@GetMapping(&quot;/test&quot;)
public Mono&lt;Test&gt; test() {
   testRepository.findById(3L).subscribe(v-&gt;System.out.println(&quot;Value: &quot; + 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中安装它。

当我手动创建gettersetter方法时,它起作用了。

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(&quot;localhost&quot;)
                .port(5432)
                .username(&quot;username&quot;)
                .password(&quot;password&quot;)
                .database(&quot;mydb&quot;)
                .build());
    }

}

Then in the repository:

@Repository
public class MyRepository {

    @Autowired
    private DatabaseClient client;


    public Flux&lt;String&gt; 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&lt;String&gt; mono = db.getData();
mono.subscribe(value -&gt; System.out.println(value));

huangapple
  • 本文由 发表于 2020年7月22日 02:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定