0

我正在尝试使用弹簧数据和 r2dbc 运行简单的弹簧启动应用程序,但是当我运行选择查询时,它确实返回任何记录。

配置

@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的1

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);
}

Spring Boot 的主类

@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();
    }
}
4

0 回答 0