Java 如何正确地提供CRUD存储库,以便能够在DB中查看和创建数据(bean出现错误I&\x27;m)?

Java 如何正确地提供CRUD存储库,以便能够在DB中查看和创建数据(bean出现错误I&\x27;m)?,java,spring,spring-boot,Java,Spring,Spring Boot,我开始学习用Java编程,面临着我几天都无法解决的问题。我使用JDK-13和Spring Boot 2.4.4,当我尝试在项目中添加CRUD存储库并启动服务器时,我得到一个错误: Description: Field noteRepo in com.javatechnologies.zettelkasten.MainController required a bean of type 'com.javatechnologies.zettelkasten.NoteRepository' that

我开始学习用Java编程,面临着我几天都无法解决的问题。我使用JDK-13和Spring Boot 2.4.4,当我尝试在项目中添加CRUD存储库并启动服务器时,我得到一个错误:

Description:

Field noteRepo in com.javatechnologies.zettelkasten.MainController required a bean of type 'com.javatechnologies.zettelkasten.NoteRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.javatechnologies.zettelkasten.NoteRepository' in your configuration.
下面提供的github存储库的附加信息和链接。如果有人能帮助我,我将非常感激

我有一个db模型:


@Entity
public class Note {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String title;
    private String text;
    private String tag;

    // empty constructor for spring model generation
    public Note(){
    }

    public Note(String title, String text, String tag){
        this.title = title;
        this.text = text;
        this.tag = tag;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

和此存储库:

package com.javatechnologies.zettelkasten;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;

@Component
public interface NoteRepository extends CrudRepository<Note, Long> {}
package com.javatechnologies.zettelkasten;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Component;
@组成部分
公共接口NoteRepository扩展了Crudepository{}
现在我尝试在控制器中使用此存储库:

@Controller
public class MainController {
    @Autowired
    private NoteRepository noteRepo;

    @GetMapping("/")
    public String mainPaige(Map<String, Object> model) {
        Iterable<Note> notes = noteRepo.findAll();

        model.put("notes", notes);
        return "mainPage";
    }
...
@控制器
公共类主控制器{
@自动连线
私人票据回购;
@GetMapping(“/”)
公共字符串mainPaige(映射模型){
Iterable notes=noteRepo.findAll();
模型。投入(“注释”,注释);
返回“主页”;
}
...
如果需要其他信息,可以访问github上的存储库:

@RestController
@请求映射(“/note”)
公共类主控制器{
@自动连线
私人票据回购;
@GetMapping(“/”)
公共字符串mainPaige(映射模型){
Iterable notes=noteRepo.findAll();
模型。投入(“注释”,注释);
返回“主页”;
}
...

如果您正在尝试使用JAP,您将错过pom中的
spring boot starter数据jpa
依赖项:

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

并用
@repository

注释存储库。不要添加代码ony。解释您所做的工作并why@zakariadaoudi它没有解决我的问题。我得到了相同的错误
NoteRepository
必须用
@Repository
@Jens注释@Repository没有解决错误。我试图添加
@Repository
代替了
@Component
,并尝试添加
@Component
@Repository
——它不会影响扩展Crudepository时的错误@rЯЯаааЯЯаЯааааааааа是依赖项:“spring boot starter data jpa”。
EnableJpaRepositories
是不必要的,也不需要添加
@Repository
注释,因为通过扩展
crudepository
您已经对spring说要创建一个bean@Jens非常感谢!它解决了我的问题。@不客气
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
@SpringBootApplication
@EnableJpaRepositories
public class ZettelkastenApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZettelkastenApplication.class, args);
    }
}