Java Spring JPA 存储库 [英] Java Spring JPA Repository

查看:41
本文介绍了Java Spring JPA 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个 Spring 菜鸟,正在为此苦苦挣扎.

基本上在开始使用 Spring 和 JPA 开发我的服务器之前,我试图开始一个简单的例子来适应这个框架.我已经成功地让 Spring 使用了一些框架,如 Log4J、Swagger 和其他框架.现在我正在尝试使用 JPA,我可以找到一些解决方案.

我看到了一些关于如何使用它进行开发的博客,我从成千上万的选项中选择创建我的存储库接口和扩展存储库.你可以在下面看到我的代码:

package com.example.model;@实体公共类人{@Id公共整数 ID;公共字符串名称;公众人物(){}}包 com.example.repository;公共接口 PersonRepository 扩展了 Repository{集合<人>findAll();}包 com.example.controller;@RestController公共类 PersonController {@自动连线私人 PersonRepository 存储库;@RequestMapping(value = "/persons", method = RequestMethod.GET)公共收藏<人>获取全部(){返回 repo.findAll();}}包com.example;@SpringBootApplication公共类应用{公共静态无效主(字符串 [] args){SpringApplication.run(App.class, args);}}

而且我还有 application.properties 文件:

spring.datasource.platform=postgresspring.datasource.url=jdbc:postgresql://localhost:5432/test_dbspring.datasource.username=testspring.datasource.password=testspring.datasource.driver-class-name=org.postgresql.Driver

当我让服务器运行时,出现以下异常:

:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名为personController"的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.example.repository.PersonRepository com.example.controllers.PersonController.repo;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [com.example.repository.PersonRepository] ​​的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解::关闭持久性单元默认"的 JPA EntityManagerFactory: 停止服务Tomcat: 应用程序启动失败org.springframework.beans.factory.BeanCreationException:创建名为personController"的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.example.repository.PersonRepository com.example.controllers.PersonController.repo;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [com.example.repository.PersonRepository] ​​的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

我创建了一个 Github 存储库以在

另请参阅 JPA 的 spring 指南

I'm a Spring noob and I'm struggling with it.

Basically before starting develop my Server with Spring in conjunction with JPA I tried to start a simple example just to get used to this framework. I've already get succeded in make Spring working with some frameworks as Log4J, Swagger and others. Now I'm trying to work with JPA and there are some points i can find out the solution.

I saw some blogs on how to develop with it and from all thousands options i choose to create my Repository Interfece and extend Repository<T, ID>. You can see my code bellow:

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);
    }
}

And I also have the application.properties file:

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

When I put the server running I get the following exception:

: 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)}

I created a Github Repository to share the code here.

Any clue about what am I doing wrong?

解决方案

First thing here is why you need to implements the base interface Repository as doing so, you will not have usual CRUD operations. For such operations is better to implements CrudRepository. Since you implement CrudRepository no need to define a findAll() Method and many well known others you can find in doc mentioned.

Furthermore, when using @SpringBootApplication Spring boot use default values. If you see the @SpringBootApplication definition you will see that :

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: [...]

That means when using default values for ComponentScan your packages sturctures shloud be as following :

  1. com.example.model -> your entities
  2. com.example.repositoriy -> your repositories
  3. com.example.controller -> controllers
  4. com.example -> MainApplication class

Here is an example for default project structure The main Application Class should be in higher level package then the others. Unless you have to specify packages location with @ComponentScan.

As you are beginner with the framework. I suggest you to always see classes definitions in official documentation.

UPDATE :

Here is an example from one of my spring boot projects

Also see this spring guide for JPA

这篇关于Java Spring JPA 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆