我正在尝试使用spring数据和r2dbc运行简单的spring boot应用程序,但是当我运行select查询时,它确实会返回任何记录。
配置
@Configuration
@EnableR2dbcRepositories("com.ns.repository")
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
@Bean
@Override
public PostgresqlConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration
.builder()
.host("localhost")
.database("employee")
.username("postgres")
.password("nssdw")
.port(5432)
.build());
}
@Bean
public DatabaseClient databaseClient(ConnectionFactory connectionFactory) {
return DatabaseClient.builder().connectionFactory(connectionFactory).build();
}
@Bean
R2dbcRepositoryFactory repositoryFactory(DatabaseClient client) {
RelationalMappingContext context = new RelationalMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context, new DefaultReactiveDataAccessStrategy(new PostgresDialect()));
}
}
只是获取请求的控制器,我什至没有传递请求参数,只是将id硬编码为name_id
package com.ns.controller;
import com.ns.entities.Name;
import com.ns.repository.NameRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class EventDrivenController {
@Autowired
private NameRepository repositoryEvent;
@GetMapping(value = "/pools" )
public Mono<Name> getEmployee() {
Mono<Name> mono = repositoryEvent.findById(1);
repositoryEvent.findById(1).doOnEach(System.out::println);
return mono;
}
}
反应式存储库,很容易通过id请求获得
@Repository
public interface NameRepository extends ReactiveCrudRepository<Name,Integer> {
@Query( "SELECT name_id, last_name, first_name FROM name WHERE name_id = $1")
Mono<Name> findById(Integer id);
}
正在调用get调用的webclient
public void callwebclient() {
WebClient client = WebClient.create("http://localhost:8080");
Mono<Name> nameMono = client.get()
.uri("/pools")
.retrieve()
.bodyToMono(Name.class);
nameMono.subscribe(System.out::println);
}
弹簧靴的主类
@SpringBootApplication
public class EventDrivenSystemApplication implements CommandLineRunner {
@Autowired
NameRepository nameRepository;
public static void main(String[] args) {
SpringApplication.run(EventDrivenSystemApplication.class, args);
NameWebClient nameWebClient = new NameWebClient();
nameWebClient.callwebclient();
}
}
正在调用webclient的主类。 Webclient中的打印语句不打印任何内容
@ComponentScan(basePackages ={"com.ns"})
@SpringBootApplication
@EnableR2dbcRepositories
public class EventDrivenSystemApplication {
public static void main(String[] args) {
SpringApplication.run(EventDrivenSystemApplication.class, args);
NameWebClient nameWebClient = new NameWebClient();
nameWebClient.callwebclient();
}
}