Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法创建JPA/Crudepository bean_Java_Sql_Spring_Spring Mvc_Spring Data - Fatal编程技术网

Java 无法创建JPA/Crudepository bean

Java 无法创建JPA/Crudepository bean,java,sql,spring,spring-mvc,spring-data,Java,Sql,Spring,Spring Mvc,Spring Data,我正在使用SpringDataJPA创建第一次SpringREST服务 并在误差以下 应用程序无法启动 说明: com.example.demo.controller.AddProduct中的字段product_repo需要一个类型为“com.example.demo.repository.ProductRepository”的bean,但找不到该bean 注入点具有以下注释: -@org.springframework.beans.factory.annotation.Autowired(

我正在使用SpringDataJPA创建第一次SpringREST服务

并在误差以下


应用程序无法启动


说明:

com.example.demo.controller.AddProduct中的字段product_repo需要一个类型为“com.example.demo.repository.ProductRepository”的bean,但找不到该bean

注入点具有以下注释: -@org.springframework.beans.factory.annotation.Autowired(必需=true)

行动:

考虑在配置中定义“com.example.demo.repository.ProductRepository”类型的bean

我的类和接口是:

  • 控制器
  • 实体
  • 存储库

  • 在ProductRepository界面上添加@Repository注释。如果可能的话,也不要扩展CRUDEPOSITORY extendJpaRepository

    。。你能提到类的包名或添加你的项目结构吗?
    @RestController
    public class AddProduct {
        
        
        @Autowired
        private ProductRepositroy product_repo;
        
        @GetMapping("/add")
        public String addproduct() {
            
            
            Product p1 = new Product();
            p1.setId(1);
            p1.setName("Amit");
            
            
            Product p2 = new Product();
            p1.setId(2);
            p1.setName("Sumit");
            
            
            product_repo.save(p1);
            product_repo.save(p2);
            
            
            return "added successfully the recod";
            
        }
    
    }
    
    @Entity
    public class Product {
        
        @Id
        private int id;
        private String name;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
    
    }
    
    public interface ProductRepositroy extends CrudRepository<Product, Integer> {
    
    }
    
    @SpringBootApplication
    public class Demo1Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo1Application.class, args);
        }
        
        
    
    }