找不到 Spring Boot 存储库(Hibernate)

SpringBoot repository not found (Hibernate)

您好,我正在使用 spring-boot 和休眠创建简单的 webapp。 application.properties 文件包含数据库连接的属性。 下面是我的文件:

应用上下文:

package hbwc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

客户实体:

package hbwc.customer;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;

    protected Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

 }

客户资料库:

package hbwc.customer;

import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {}

客户控制器:

package hbwc.customer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class CustomerController {

    @Autowired
    CustomerRepository repository;

    @RequestMapping("/customers")
        public String index() {
        List<Customer> all = (List<Customer>) repository.findAll();
        return "ok";
    }
}

我正在使用 gradlew 来构建和 运行ning 这个例子。但是我有问题 运行宁它 (./gradlew 运行)。我遇到异常,告诉我找不到我的 CustomerRepository。 堆栈跟踪:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hbwc.customer.CustomerRepository] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 18 common frames omitted

请给我一些建议我的代码有什么问题。

application.properties(项目根目录)

spring.datasource.url=jdbc:mysql://localhost/hbwc
spring.datasource.username=root
spring.datasource.password=baza1

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop

您的存储库中没有任何类型的组件注释 class。尝试添加@Repository 或@Component。然后你应该(假设启用了组件扫描)能够将存储库自动连接到你的控制器中。