程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Spring Boot Data Jpa应用程序中使用条件查询大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到如何在Spring Boot Data Jpa应用程序中使用条件查询的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在Spring Boot Data Jpa应用程序中使用条件查询的解决方法建议,希望对你解决如何在Spring Boot Data Jpa应用程序中使用条件查询有所启发或帮助;

来自 docs

像这样定义一个接口

public interface studentRepositoryCustom {

    List<String> nameByCourse(String courseName);

}

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

@service
class 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

解决方法

我有一个使用 Spring Boot Data jpa 的应用程序。到目前为止,我正在使用这样的存储库

public interface studentRepository extends CrudRepository<studentEntity,Integer>{
    @Query(value = "" 
        + "SELECT s.studentname "
        + "FROM   studententity s,"
        + "       courseentity c "
        + "WHERE  s.coursEID = c.coursEID "
        + "       AND s.coursEID IN (SELECT c.coursEID "
        + "                          FROM   courseentity c "
        + "                          WHERE  c.coursename = ?1)")
    List<String> nameByCourse(String courseName);
}

我如何在Spring Boot应用程序中利用Hibernate为此类情况提供的 条件查询

@H_673_46@

大佬总结

以上是大佬教程为你收集整理的如何在Spring Boot Data Jpa应用程序中使用条件查询全部内容,希望文章能够帮你解决如何在Spring Boot Data Jpa应用程序中使用条件查询所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。