Hibernate 从扩展crudepository的接口创建bean<;类别,ID>;?

Hibernate 从扩展crudepository的接口创建bean<;类别,ID>;?,hibernate,spring-boot,spring-data-jpa,Hibernate,Spring Boot,Spring Data Jpa,在将Hibernate实施到项目中之前,我将按照本教程进行测试: 我的问题是,此指令要求UserDAO是bean: @Autowired private UserDao userDao; 哪个不是?我觉得我在这个网站提供的例子中遗漏了一些非常重要的东西。我习惯于使用implements Serializable实现bean,但在这个场景中不使用它。它怎么会被认为是豆子呢 如果有人能向我解释我错过了什么,我将不胜感激。这种仅使用接口和标准方法声明函数来实现CRUD的方法看起来非常吸引人。谢谢大家

在将Hibernate实施到项目中之前,我将按照本教程进行测试:

我的问题是,此指令要求UserDAO是bean:

@Autowired
private UserDao userDao;
哪个不是?我觉得我在这个网站提供的例子中遗漏了一些非常重要的东西。我习惯于使用
implements Serializable
实现bean,但在这个场景中不使用它。它怎么会被认为是豆子呢

如果有人能向我解释我错过了什么,我将不胜感激。这种仅使用接口和标准方法声明函数来实现CRUD的方法看起来非常吸引人。谢谢大家!

编辑:下面是我的代码。当我试图将教程中提供的代码集成到我的项目中时,由于项目体系结构的不同(这是我的猜测),该教程中提供的代码不起作用

架构:

Application.java

@SpringBootApplication
@ComponentScan("boot")
@ComponentScan("dao")
@ComponentScan("modele")
@EnableJpaRepositories("org.utc.ai15")
public class Application {

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

}
@Transactional
@Component 
public interface TestDAO extends CrudRepository<Test, Long > {

  /**
   * This method will find an User instance in the database by its email.
   * Note that this method is not implemented and its working code will be
   * automagically generated from its signature by Spring Data JPA.
   */
  public Test findByEmail(String email);

}
TestDAO.java

@SpringBootApplication
@ComponentScan("boot")
@ComponentScan("dao")
@ComponentScan("modele")
@EnableJpaRepositories("org.utc.ai15")
public class Application {

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

}
@Transactional
@Component 
public interface TestDAO extends CrudRepository<Test, Long > {

  /**
   * This method will find an User instance in the database by its email.
   * Note that this method is not implemented and its working code will be
   * automagically generated from its signature by Spring Data JPA.
   */
  public Test findByEmail(String email);

}
@Transactional
@组成部分
公共接口TestDAO扩展了crudepository{
/**
*此方法将通过电子邮件在数据库中查找用户实例。
*请注意,此方法未实现,其工作代码将被删除
*由Spring Data JPA根据其签名自动生成。
*/
公共测试findByEmail(字符串电子邮件);
}
我认为这就是问题的全部原因

我尝试将
@Component
添加到TestDAO.java中,因此使用
@ComponentScan(“dao”)
扫描它,但它不起作用

以下是错误:

boot.controller.TestController中的字段testDao需要找不到类型为“dao.testDao”的bean。


编辑2:错误是
@EnableJpaRepositories(“org.utc.ai15”)
,正确的声明是
@EnableJpaRepositories(“dao”)
(cf@Alex-answer)。

很可能是由于bean发现/包扫描

检查以下各项:

  • 或者确保您的UserDAO位于springboot应用程序下面(springboot使用主类作为根来扫描bean)
  • 或者使用
    @EnableJpaRepositories(basePackages=“org.my.pkg”)
  • 或者添加
    @ScanPackages
    ,为bean发现定义额外的包(不仅仅是存储库)

通过,我指的是你在类的顶端拥有的东西。

userDao是一个“存储库”bean。也许您应该阅读SpringDataJPA文档。您的教程提到:“使用Spring数据JPA,只需扩展Spring提供的Crudepository接口即可创建实体的DAO”。简而言之,Spring数据仅通过创建一个接口就为您提供了随时可用的存储库。然后可以将其作为bean注入,并使用save/delete/find方法……感谢您的回复。我更了解正在发生的事情。但我仍然无法创建UserDAO的bean。它只是不被认作豆子。我已经用@EnableJpaRepositories进行了注释,遵循了与文档中定义的步骤相同的教程。你知道会出什么问题吗?明天我将发布我的代码示例,但它的UserDAO是相同的。我敢打赌这与您的包有关。您确实有一个带有main方法的
@springboot应用程序
?这就是您注释的
@EnableJpaRepositories
?您可以确保UserDAO位于springboot应用程序之下(springboot使用主类作为根来扫描bean),或者使用
@EnableJpaRepositories(basePackages=“org.my.pkg”)
定义存储库包,或者添加
@ScanPackages
来定义用于bean发现的其他包(不仅仅是存储库)。这只是一个猜测(基于我所看到的常见错误)。我明天会检查所有这些。再次感谢。