如何在Spring Boot Data Jpa应用程序中使用条件查询

如何在Spring Boot Data Jpa应用程序中使用条件查询,第1张

如何在Spring Boot Data Jpa应用程序中使用条件查询

来自

docs

要使用自定义功能丰富存储库,您首先要定义自定义功能的接口和实现。使用您提供的存储库界面扩展自定义界面。

像这样定义一个接口

public interface StudentRepositoryCustom {    List<String> nameByCourse(String coursename);}

然后像这样定义此接口的自定义实现

@Serviceclass StudentRepositoryImpl implements StudentRepositoryCustom {    @PersistenceContext    private EntityManager em;    public List<String> nameByCourse(String coursename) {         CriteriaBuilder cb = em.getCriteriaBuilder();        //Using criteria builder you can build your criteria queries.    }}

现在,您可以像这样在JPA存储库中扩展此自定义存储库实现。

public interface StudentRepository extends CrudRepository<StudentEntity, Integer>, StudentRepositoryCustom {}

了解有关条件查询和条件构建器的更多信息

here



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5124388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存