Spring boot 如何在spring引导应用程序中自动连接SimpleParepository?

Spring boot 如何在spring引导应用程序中自动连接SimpleParepository?,spring-boot,spring-data-jpa,Spring Boot,Spring Data Jpa,我正在从事一个项目,该项目使用SpringBoot 2.0.5版本SpringDataJPA来使用JPA持久化和检索记录。我在服务层中自动连接SimpleParepository。但在启动我的应用程序时,它失败了 "NoSuchBeanDefinitionException"- No qualifying bean of type 'org.springframework.data.jpa.repository.support.SimpleJpaRepository<?, ?>'

我正在从事一个项目,该项目使用SpringBoot 2.0.5版本SpringDataJPA来使用JPA持久化和检索记录。我在服务层中自动连接SimpleParepository。但在启动我的应用程序时,它失败了

"NoSuchBeanDefinitionException"- No qualifying bean of type 
'org.springframework.data.jpa.repository.support.SimpleJpaRepository<?, ?>' 
 available: expected at least 1 bean which qualifies as autowire candidate. 
我的方法不对吗?这不是自动连接SimpleParepository的正确方法吗

现在,我没有必要像Spring那样扩展SimpleParepository,只要JPARepository对我有好处


谢谢

您仍然需要创建一个扩展JpaRepisitory或您选择的spring存储库类型的存储库接口

要引用spring数据,请执行以下操作:

1.2.1定义存储库接口

作为第一步,您需要定义特定于域类的存储库接口。接口必须扩展 存储库和存储库必须键入域类和ID类型。如果你 要公开该域类型的CRUD方法,请扩展 crudepository代替了Repository


一旦您创建了一个新的存储库类型,您将按照该类型而不是SimpleParepository自动连线。

您应该创建一个扩展JpaRepisitory的存储库接口

@Repository
public interface MyRepository extends JpaRepisitory<T, ID> {
    //
}
@存储库
公共接口MyRepository扩展了JpaRepisitory{
//
}
您应该在服务类中自动连接

@Service("service")
public class MyServiceImpl<V,K> implements MyService<V,K>{

  @Autowired
  private MyRepository myRepository;

}
@服务(“服务”)
公共类MyServiceImpl实现了MyService{
@自动连线
私有MyRepository MyRepository;
}

获得SimpleParepository实现的一种方法是使用配置类创建一个实例,作为将在服务中使用的bean

@Configuration
public class PersistanceConfiguration {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public SimpleJpaRepository<YourEntity, Long> getYourEntitySimpleRepository() {
        return new SimpleJpaRepository<>(YourEntity.class, entityManager);
    }
}
@配置
公共类持久化配置{
@持久上下文
私人实体管理者实体管理者;
@豆子
公共SimpleParepository getYourEntitySimpleRepository(){
返回新的SimpleParepository(YourEntity.class,entityManager);
}
}
并将其注入到您的服务中,就像使用JpaRepository一样,例如:

@Service
public class YourEntityServiceImpl<YourEntity, Long> implements YourEntityService {

    private JpaRepository<YourEntity, K> repository;
    private SimpleJpaRepository<YourEntity, K> simpleRepository;

    @Autowired
    public YourEntityServiceImpl(YourEntityRepository repository, SimpleJpaRepository<YourEntity, Long> simpleRepository) {
        this.repository = repository;
        this.simpleRepository = simpleRepository;
    }

}
@服务
公共类YourEntityServiceImpl实现YourEntityService{
私有JPA存储库;
私人单纯形;
@自动连线
public YourEntityServiceImpl(YourEntityRepository存储库,SimpleParepository simpleRepository){
this.repository=存储库;
this.simpleRepository=simpleRepository;
}
}

SimpleParepository是JPARepository的一个实现,根据其文档,我相信可以按开箱即用的方式使用。那么,创建另一个接口的原因是什么呢。另外,根据您的示例,接口如何扩展类。
@Service("service")
public class MyServiceImpl<V,K> implements MyService<V,K>{

  @Autowired
  private MyRepository myRepository;

}
@Configuration
public class PersistanceConfiguration {

    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public SimpleJpaRepository<YourEntity, Long> getYourEntitySimpleRepository() {
        return new SimpleJpaRepository<>(YourEntity.class, entityManager);
    }
}
@Service
public class YourEntityServiceImpl<YourEntity, Long> implements YourEntityService {

    private JpaRepository<YourEntity, K> repository;
    private SimpleJpaRepository<YourEntity, K> simpleRepository;

    @Autowired
    public YourEntityServiceImpl(YourEntityRepository repository, SimpleJpaRepository<YourEntity, Long> simpleRepository) {
        this.repository = repository;
        this.simpleRepository = simpleRepository;
    }

}