java—如何正确地提供crud存储库,以便能够在db中查看和创建数据(beans出现错误)?

wf82jlnq  于 2021-07-16  发布在  Java
关注(0)|答案(2)|浏览(164)

我开始学习用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> {}

现在我尝试在我的控制器中使用这个存储库:

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

如果需要其他信息,可以访问github上的存储库:https://github.com/olegyariga/zettelkasten

zwghvu4y

zwghvu4y1#

如果你是日本人,你会错过 spring-boot-starter-data-jpa pom中的依赖关系:

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

你必须加上 @EnableJpaRepositories 你的主要课程:

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

并用 @Repository

lf5gs5x2

lf5gs5x22#

@RestController
@RequestMapping("/note")
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";
    }
...

相关问题