春季数据R2DBC:连接到mySQL的问题 - 找不到存储库bean

发布于 2025-01-24 15:13:02 字数 2971 浏览 3 评论 0原文

我正在尝试使用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.

application.yaml: 尝试定义spring.r2dbc.pool.enabled:false and 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);
    }
}

任何帮助都将不胜感激。

I am trying to integrate MySql to spring boot application using r2dbc driver. While doing so encountering issue wherein the repository bean is not getting created. I saw similar questions but the approaches mentioned in those didn't help.

Error message below:

***************************
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.

application.yaml:
Tried to define spring.r2dbc.pool.enabled: false and spring.r2dbc.pool.enabled:. But both of this didn't help

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 dependencies


    <!--  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

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

Student entity


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

Main class
I used @EnableR2dbcRepositories as well below but it didn't help much and getting same issue

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

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2025-01-31 15:13:02

在您的StudentRepository接口中,扩展r2dbcrepository而不是eactivevecrudrepository

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

In your StudentRepository interface, extend R2dbcRepository instead of ReactiveCrudRepository.

I recently had the same issue and that fixed it.

祁梦 2025-01-31 15:13:02

您在学生pojo中使用uuid作为ID,但是在存储库定义中,您使用long。将其更改为uuid如下

 ReactiveCrudRepository<Student, UUID>

You are using UUID as id in the Student POJO, but in repository definition you use Long. Change it to UUID as follows

 ReactiveCrudRepository<Student, UUID>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文