如何解决此问题“考虑在配置中定义“repository.InterfaceName”类型的 bean。”

Cha*_*bFI 2 java intellij-idea spring-boot

我正在使用IntelliJ学习spring boot,在运行代码时遇到了这个问题

\n
2021-09-05 12:55:48.665 ERROR 10396 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : \n\n***************************\nAPPLICATION FAILED TO START\n***************************\n\nDescription:\n\nParameter 0 of constructor in service.EmployeeService required a bean of type \'repository.EmployeeRepository\' that could not be found.\n\n\nAction:\n\nConsider defining a bean of type \'repository.EmployeeRepository\' in your configuration.\n\n\nProcess finished with exit code 0\n
Run Code Online (Sandbox Code Playgroud)\n

项目文件树如下所示:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 HELP.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw.cmd\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my-first-project.iml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pom.xml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplication.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 controller\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeController.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 model\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Employee.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 repository\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeRepository.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 service\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeService.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 static\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82\xc2\xa0\xc2\xa0             \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0                 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0                     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplicationTests.java\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 target\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 classes\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 com\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplication.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 controller\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeController.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 model\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Employee.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 repository\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeRepository.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 service\n    \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeService.class\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 generated-sources\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 annotations\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 generated-test-sources\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test-annotations\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test-classes\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n            \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n                \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n                    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplicationTests.class\n\n
Run Code Online (Sandbox Code Playgroud)\n

员工控制器:

\n
2021-09-05 12:55:48.665 ERROR 10396 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : \n\n***************************\nAPPLICATION FAILED TO START\n***************************\n\nDescription:\n\nParameter 0 of constructor in service.EmployeeService required a bean of type \'repository.EmployeeRepository\' that could not be found.\n\n\nAction:\n\nConsider defining a bean of type \'repository.EmployeeRepository\' in your configuration.\n\n\nProcess finished with exit code 0\n
Run Code Online (Sandbox Code Playgroud)\n

员工服务:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 HELP.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mvnw.cmd\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 my-first-project.iml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pom.xml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplication.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 controller\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeController.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 model\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Employee.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 repository\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeRepository.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 service\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeService.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 static\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82\xc2\xa0\xc2\xa0             \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0                 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0                     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplicationTests.java\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 target\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 classes\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 com\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplication.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 controller\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeController.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 model\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Employee.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 repository\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeRepository.class\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 service\n    \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 EmployeeService.class\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 generated-sources\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 annotations\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 generated-test-sources\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test-annotations\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test-classes\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n            \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 firstproject\n                \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 myfirstproject\n                    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFirstProjectApplicationTests.class\n\n
Run Code Online (Sandbox Code Playgroud)\n

员工 :

\n
package controller;\n\nimport model.Employee;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport service.EmployeeService;\n\nimport java.util.List;\n\n@RestController\n@RequestMapping(path = "/api/v1/employee")\npublic class EmployeeController {\n\n    private final EmployeeService employeeService;\n\n    @Autowired\n    public EmployeeController(EmployeeService employeeService) {\n\n        this.employeeService = employeeService;\n    }\n    @GetMapping\n    public List<Employee> getEmployee(){\n        return employeeService.getEmployee();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

员工存储库:

\n
package service;\n\nimport model.Employee;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport repository.EmployeeRepository;\n\nimport java.util.List;\n\n\n@Service\npublic class EmployeeService {\n\n    final EmployeeRepository employeeRepository;\n    @Autowired\n    public EmployeeService(EmployeeRepository employeeRepository) {\n        this.employeeRepository = employeeRepository;\n    }\n\n    @GetMapping\n    public List<Employee> getEmployee(){\n\n        return employeeRepository.findAll();\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的第一个项目申请:

\n
package model;\n\nimport javax.persistence.*;\nimport java.time.LocalDate;\n@Entity\n@Table\npublic class Employee {\n    @Id\n    @SequenceGenerator(name = "emp_sequence",\n    sequenceName = "emp_sequence",\n    allocationSize = 1)\n    @GeneratedValue(strategy = GenerationType.SEQUENCE,\n            generator = "emp_sequence")\n    private Long id;\n    private String name;\n    private String email;\n    private LocalDate dob;\n    private Float salary;\n\n    public Employee() {\n    } ...\\\\Getters and Setters\n
Run Code Online (Sandbox Code Playgroud)\n

应用程序属性:

\n
spring.datasource.url=jdbc:postgresql://localhost:5432/employee\nspring.datasource.username=postgres\nspring.datasource.password=*********\nspring.jpa.hibernate.ddl-auto=create-drop\nspring.jpa.show-sql=true\nspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect\nspring.jpa.properties.hibernate.format_sql=true\nspring.jpa.open-in-view=false\n
Run Code Online (Sandbox Code Playgroud)\n

最后是 pom.xml

\n
package repository;\n\nimport model.Employee;\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.stereotype.Repository;\n\n@Repository\npublic interface EmployeeRepository extends JpaRepository<Employee,Long> {\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我在用:

\n
    \n
  • 爪哇11
  • \n
  • 春季启动(v2.5.4)
  • \n
  • Intellij IDEA 2021.2.1 终极版
  • \n
\n

我对其他软件包也有同样的问题,但添加@SpringBootApplication(scanBasePackages = {"controller","service","model"})解决了他们的问题。

\n

Pan*_*kos 6

Spring 的注释扫描不包括包存储库,因此它不会扫描@Repository将其添加为 spring bean。您还需要定义

@SpringBootApplication(scanBasePackages = {"controller","service","model", "repository"})
Run Code Online (Sandbox Code Playgroud)

原因是 spring 扫描的默认包是定义启动 spring boot 应用程序的 main 方法的com.firstproject.myfirstproject地方。MyFirstProjectApplication.claass

考虑到所有其他包都在该包之外,您需要手动定义它们,scanBasePackages以便 spring 进入其他包(如服务、存储库)并扫描注释并执行必要的操作。