Spring boot 如何在spring boot项目中访问服务中的实体管理器?

Spring boot 如何在spring boot项目中访问服务中的实体管理器?,spring-boot,spring-data,Spring Boot,Spring Data,关于如何在SpringBoot中访问EntityManager,我在Google上搜索了很多次,并按照帖子所说的做了,但都没用。我想访问实体管理器,以便执行一些自定义查询操作。在这里,我在名为“customrepository”的依赖包中定义了自定义接口: public interface PostRepositoryCustom { void refresh(Post post); } 然后我在另一个名为“customrepositoryimpl”的包中实现了这个接口: publi

关于如何在SpringBoot中访问EntityManager,我在Google上搜索了很多次,并按照帖子所说的做了,但都没用。我想访问实体管理器,以便执行一些自定义查询操作。在这里,我在名为“customrepository”的依赖包中定义了自定义接口:

public interface PostRepositoryCustom {
     void refresh(Post post);
}
然后我在另一个名为“customrepositoryimpl”的包中实现了这个接口:

public class CustomPostRepositoryImpl implements PostRepositoryCustom {

  @PersistenceContext
  private EntityManager em;

  @Override
  public void refresh(Post post) {
    em.refresh(post);       
  }
}
最后,我定义了一个标准存储库接口,它扩展了“Crudepository”和自定义存储库:

public interface PostRepository extends CrudRepository<Post,Long>,PostRepositoryCustom {}

为什么??有人告诉我应该在哪里更正错误吗?

Spring找不到bean,所以请尝试使用@Repository注释

@Repository
public class CustomPostRepositoryImpl implements PostRepositoryCustom {

  @PersistenceContext
  private EntityManager em;

  @Override
  public void refresh(Post post) {
    em.refresh(post);       
  }
}

@存储库
公共接口PostRepository扩展了Crudepository{
}

您是否有我们可以查看的回购协议?为什么您的存储库没有注释?也许你可以用
@NoRepositoryBean
注释
PostRepositoryCustom
,用
@Repository
注释
CustomPostRepositoryImpl
。无法创建
postRepository
的另一个原因是它没有提供默认实现。当调用
refresh
时,spring如何知道该做什么?与
PostRepositoryCustom
@Repository
public class CustomPostRepositoryImpl implements PostRepositoryCustom {

  @PersistenceContext
  private EntityManager em;

  @Override
  public void refresh(Post post) {
    em.refresh(post);       
  }
}
@Repository
public interface PostRepository extends CrudRepository<Post,Long>{

}