Java Spring JPA 存储库
Java Spring JPA Repository
我是一个 Spring 菜鸟,我正在为此苦苦挣扎。
基本上,在开始使用 Spring 结合 JPA 开发我的服务器之前,我尝试开始一个简单的示例,只是为了习惯这个框架。
我已经在 make Spring 中成功使用了一些框架,如 Log4J、Swagger 和其他框架。现在我正在尝试使用 JPA,并且有一些要点我可以找到解决方案。
我看到了一些关于如何使用它进行开发的博客,我从成千上万的选项中选择创建我的存储库界面和 extend Repository<T, ID>
。您可以在下面看到我的代码:
package com.example.model;
@Entity
public class Person {
@Id
public Integer id;
public String name;
public Person(){}
}
package com.example.repository;
public interface PersonRepository extends Repository<Person, Integer> {
Collection<Person> findAll();
}
package com.example.controller;
@RestController
public class PersonController {
@Autowired
private PersonRepository repo;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public Collection<Person> getAll() {
return repo.findAll();
}
}
package com.example;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我还有 application.properties 文件:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
当我将服务器 运行 放入时,出现以下异常:
: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
: Closing JPA EntityManagerFactory for persistence unit 'default'
: Stopping service Tomcat
: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我创建了一个 Github 存储库来共享代码 here。
关于我做错了什么的任何线索?
只需使用@Repository 注释您的界面。如果这不起作用,请尝试将 @EnableJPARepositories 添加到主 class.
尝试将 @Repository 注释添加到您的 PersonRepository,这可能是它找不到它的原因。
这里的第一件事是为什么您需要实现基本接口 Repository as doing so, you will not have usual CRUD operations. For such operations is better to implements CrudRepository. Since you implement CrudRepository 无需定义 findAll() 方法和您可以在提到的文档中找到许多众所周知的其他方法。
此外,当使用@SpringBootApplication
Spring引导时使用默认值。如果你看到 @SpringBootApplication definition 你会看到:
Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]
这意味着当使用 ComponentScan 的默认值时,您的包结构应该如下所示:
- com.example.model -> 你的实体
- com.example.repositoriy -> 您的存储库
- com.example.controller -> 控制器
- com.example -> MainApplication class
这里是 default project structure 的例子
主应用程序 Class 应该在比其他应用程序更高级别的包中。除非您必须使用 @ComponentScan 指定包位置。
因为您是该框架的初学者。我建议您始终在官方文档中查看 classes 定义。
更新 :
这是我的 spring 引导项目之一的示例
另请参阅此 spring guide 用于 JPA
您需要使用@Respository 注释您的 PersonRepository 接口,否则 spring 上下文将无法识别它或为其创建实现。
我是一个 Spring 菜鸟,我正在为此苦苦挣扎。
基本上,在开始使用 Spring 结合 JPA 开发我的服务器之前,我尝试开始一个简单的示例,只是为了习惯这个框架。 我已经在 make Spring 中成功使用了一些框架,如 Log4J、Swagger 和其他框架。现在我正在尝试使用 JPA,并且有一些要点我可以找到解决方案。
我看到了一些关于如何使用它进行开发的博客,我从成千上万的选项中选择创建我的存储库界面和 extend Repository<T, ID>
。您可以在下面看到我的代码:
package com.example.model;
@Entity
public class Person {
@Id
public Integer id;
public String name;
public Person(){}
}
package com.example.repository;
public interface PersonRepository extends Repository<Person, Integer> {
Collection<Person> findAll();
}
package com.example.controller;
@RestController
public class PersonController {
@Autowired
private PersonRepository repo;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public Collection<Person> getAll() {
return repo.findAll();
}
}
package com.example;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我还有 application.properties 文件:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
当我将服务器 运行 放入时,出现以下异常:
: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
: Closing JPA EntityManagerFactory for persistence unit 'default'
: Stopping service Tomcat
: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我创建了一个 Github 存储库来共享代码 here。
关于我做错了什么的任何线索?
只需使用@Repository 注释您的界面。如果这不起作用,请尝试将 @EnableJPARepositories 添加到主 class.
尝试将 @Repository 注释添加到您的 PersonRepository,这可能是它找不到它的原因。
这里的第一件事是为什么您需要实现基本接口 Repository as doing so, you will not have usual CRUD operations. For such operations is better to implements CrudRepository. Since you implement CrudRepository 无需定义 findAll() 方法和您可以在提到的文档中找到许多众所周知的其他方法。
此外,当使用@SpringBootApplication
Spring引导时使用默认值。如果你看到 @SpringBootApplication definition 你会看到:
Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]
这意味着当使用 ComponentScan 的默认值时,您的包结构应该如下所示:
- com.example.model -> 你的实体
- com.example.repositoriy -> 您的存储库
- com.example.controller -> 控制器
- com.example -> MainApplication class
这里是 default project structure 的例子 主应用程序 Class 应该在比其他应用程序更高级别的包中。除非您必须使用 @ComponentScan 指定包位置。
因为您是该框架的初学者。我建议您始终在官方文档中查看 classes 定义。
更新 :
这是我的 spring 引导项目之一的示例
另请参阅此 spring guide 用于 JPA
您需要使用@Respository 注释您的 PersonRepository 接口,否则 spring 上下文将无法识别它或为其创建实现。