我有一个mySQL数据库,按邮政编码存储有关城市的信息,包括狗和猫的数量。我想查询数据库中所有狗的数量比猫的数量多的邮政编码,并按降序排列结果。
我正在使用JpaRepository类,并且已经知道了如何创建诸如findByPopulation(int population)之类的简单查询,但是JPA是否能够创建我需要的复杂查询呢?
发布于 2019-12-17 15:13:37
您可以使用Spring JPA Specification构建复杂的查询。
我对你们的关系不太确定,但据我所知。下面的代码应该是有帮助的。
public static Specification<City> isDogsGreaterThanCats() {
return (root, query, cb) ->{
return cb.greaterThan(root.get(dog_count), root.get(cat_count));
};Repo方法调用将类似于
cityRepository.findAll(isDogsGreaterThanCats());发布于 2019-12-17 15:40:21
我建议将原生查询添加到存储库。
@Query(value="",nativeQuery = true)
findByPopulation(@Param("population") int population);在value=""中添加复杂查询
https://stackoverflow.com/questions/59366270
复制相似问题