Java 接口的Spring引导存储库
是否有可能基于Spring引导数据中的接口创建存储库? 我编写了这段代码,其中Medicamento是一个接口,许多模型都实现了它,但没有成功:Java 接口的Spring引导存储库,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,是否有可能基于Spring引导数据中的接口创建存储库? 我编写了这段代码,其中Medicamento是一个接口,许多模型都实现了它,但没有成功: public interface MedicamentoRepository extends JpaRepository<Medicamento, Long> { public <T extends Medicamento> List<Medicamento> findItens(Class<T>
public interface MedicamentoRepository extends JpaRepository<Medicamento, Long> {
public <T extends Medicamento> List<Medicamento> findItens(Class<T> type);
}
公共接口MedicamentoRepository扩展了JpaRepository{
公共列表FindItems(类类型);
}
谢谢大家! 使用Spring数据JPA,Spring负责生成实现并将其注册为Spring管理的组件。您所要做的就是通过在Spring配置类中添加
@EnableJpaReositories
并指定存储库接口所在的包来正确配置它
所有基本方法,如findOne
,findAll
,delete
。。。已由crudepository
界面(和JpaRepository
)提供,因此您无需将它们添加到界面中
如果需要添加特定的方法,Spring将根据方法的名称和参数(如findByName
,其中name
是实体的字段)或使用@Query
注释生成实现
为了更好地了解Spring Data JPA的工作原理和所有可用功能,请查看他们的文档:如果将
Medicamento
设置为抽象类会发生什么?当然,如果从repo接口生成automagic repo的过程对您的用例无效,只需创建一个自定义存储库即可。这是一个Spring Boot应用程序,如果没有findItems
方法,您的界面应该可以正常工作。