【问题标题】:Spring data r2dbc: Problem connecting to MySQL - Repository bean not foundSpring data r2dbc:连接到 MySQL 的问题 - 找不到存储库 bean
【发布时间】:2022-10-18 16:52:53
【问题描述】:

我正在尝试使用 r2dbc 驱动程序将 MySql 集成到 Spring Boot 应用程序。这样做时会遇到未创建存储库 bean 的问题。我看到了类似的问题,但其中提到的方法没有帮助。

下面的错误信息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 4 of constructor in com.abc.studentservice.utils.impl.HostelImpl required a bean of type 'com.abc.studentservice.repository.StudentRepository' that could not be found.


Action:

Consider defining a bean of type 'com.abc.studentservice.repository.StudentRepository' in your configuration.

应用程序.yaml:试图定义 spring.r2dbc.pool.enabled: false 和 spring.r2dbc.pool.enabled:。但这两者都没有帮助

spring:
  profiles:
    active: devo
  r2dbc:
    url: r2dbc:pool:mysql://localhost/student
    username: mysql
    password: mysql
    pool:
      initial-size: 10
      max-size: 50
      max-idle-time: 30m
      validation-query: SELECT 1
  data:
    r2dbc:
      repositories:
        enabled: true

Maven 依赖项


    <!--  Springboot data -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-r2dbc</artifactId>
      <version>2.4.5</version>
    </dependency>

    <!--  Enable connection pooling -->
    <dependency>
      <groupId>io.r2dbc</groupId>
      <artifactId>r2dbc-pool</artifactId>
      <version>0.8.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.6</version>
    </dependency>


    <!--  Reactive Mysql -->
    <dependency>
      <groupId>dev.miku</groupId>
      <artifactId>r2dbc-mysql</artifactId>
      <version>0.8.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.23</version>
    </dependency>

存储库

@Repository
public interface StudentRepository extends ReactiveCrudRepository<Student, UUID> {
}

学生实体


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("student")
public class Student {
    @Id
    @Column("id")
    private UUID id;

    @Column("first_name")
    private String firstName;

    @Column("last_name")
    private String lastName;
}

主班我在下面也使用了@EnableR2dbcRepositories,但它并没有太大帮助并且遇到了同样的问题

@SpringBootApplication
public class StudentserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(StudentserviceApplication.class, args);
    }
}

任何帮助,将不胜感激。

【问题讨论】:

    标签: spring-boot spring-data spring-data-r2dbc r2dbc-mysql


    【解决方案1】:

    您在 Student POJO 中使用 UUID 作为 id,但在存储库定义中使用 Long。改成UUID如下

     ReactiveCrudRepository<Student, UUID>
    

    【讨论】:

    • 我更新了相同但面临相同的问题
    【解决方案2】:

    在您的StudentRepository 界面中,扩展R2dbcRepository 而不是ReactiveCrudRepository

    我最近遇到了同样的问题,并且解决了它。

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 2020-10-24
      • 2020-12-23
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多