gpt4 book ai didi

java - 为 Spring Data JPA 创建自定义存储库

转载 作者:行者123 更新时间:2023-11-30 10:08:07 25 4
gpt4 key购买 nike

我尝试按照本教程创建一个自定义存储库:https://www.baeldung.com/spring-data-jpa-method-in-all-repositories

我的应用构建失败并出现错误:

NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.dao.ExtendedStudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的完整源代码: https://github.com/sesong11/springjpa-custom-repo

有很多类似的问题,但没有一个适合我。可能是当前版本 2.1.1 的 Spring 问题,或者我错过了一些配置。

最佳答案

要使测试正常运行,您必须执行以下操作:

1) 将 StudentJPAH2ConfigbasePackages 的错误定义替换为 com.example.demo.dao,或者最好将其删除为多余的:

@Configuration
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
public class StudentJPAH2Config {
}

2) 同时将@ComponentScan中的basePackagesDemoApplication类中的@EntityScan替换为com .example.demo.daocom.example.demo.entity。或者最好完全删除那些和 @EnableTransactionManagement 注释:

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

3) 添加no-argument constructor到实体 Student

4) 更正您的测试类 - 使用 @DataJpaTest测试 DAO 层并导入您的 StudentJPAH2Config 配置:

@RunWith(SpringRunner.class)
@DataJpaTest
@Import(StudentJPAH2Config.class)
public class ExtendedStudentRepositoryIntegrationTest {
//...
}

通过这些更正,我已成功运行您的测试。

关于java - 为 Spring Data JPA 创建自定义存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53921123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com