没有可用的 'tn.esen.dao.StudentRepository' 类型的合格 bean

No qualifying bean of type 'tn.esen.dao.StudentRepository' available

我正在用 SPRING 开发一个 crud 应用程序,和往常一样,我遇到了一个问题,说没有可用的合格 bean,甚至没有在数据库中添加学生 table。

这是问题所在:

Exception in thread "main" org.spring framework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'tn.esen.dao.StudentRepository' available

实体:

package tn.esen.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;


@Entity
@Table(name="etud")
public class Student {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    private String nom;
    
    private String prenom;
    
    private Double moyenne;

    public Long getId() {
        return id;
    }

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

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public Double getMoyenne() {
        return moyenne;
    }

    public void setMoyenne(Double moyenne) {
        this.moyenne = moyenne;
    }

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
}

控制器:

package tn.esen.control;


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import tn.esen.dao.StudentRepository;
import tn.esen.entity.Student;


@Controller
public class MycontrolStudent {

    @Autowired
    public StudentRepository metier;
    
    @RequestMapping(value="/index")
    public String home(Model mymodel) {
        
        //importing the list of all students
        List<Student> allstudents= metier.findAll();
        //adding an attribute to the model contain the list of students
        mymodel.addAttribute("listestudent",allstudents);
        return ("acceuil");
        
    }
}

存储库:

package tn.esen.dao;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import tn.esen.entity.Student;

@Repository
public interface StudentRepository extends JpaRepository<Student,Long> {
    
}

主程序:

package com.example.demo;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import tn.esen.dao.StudentRepository;
import tn.esen.entity.Student;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx =  SpringApplication.run(DemoApplication.class, args);
        
        StudentRepository StudentRepository = ctx.getBean(StudentRepository.class);
        
        
        List<Student> allstud = StudentRepository.findAll();
         System.out.println("--------------------list of students-----------------------");
         
         for(Student stud:allstud) {
             System.out.println(stud); 
         }
    }
}

您的 StudentRepository 在一个完全不同的包中:tn.esen.dao 您应该添加 @ComponentScan(basePackages = "tn.esen") 给你的 DemoApplication.java