程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用Spring Boot CrudRepository过滤数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用Spring Boot CrudRepository过滤数据?

开发过程中遇到使用Spring Boot CrudRepository过滤数据的问题如何解决?下面主要结合日常开发的经验,给出你关于使用Spring Boot CrudRepository过滤数据的解决方法建议,希望对你解决使用Spring Boot CrudRepository过滤数据有所启发或帮助;

一种替代方案,可以通过标准API或queryDSL使用规范模式,可以解决您在上述注释中必须为每种参数组合创建查询方法的问题。

出于对以下方面的关注,下面概述了两种方法:

https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications- and-querydsl/

我发现queryDSL易于使用。您只需要定义一个接口方法,然后就可以将参数的任意组合传递给谓词。

例如

public interface UserRepository extends PagingAndSorTingRepository<User, Long>, queryDslPreDicateExecutor<User> {
    public List<User> findAll(PreDicate preDicatE);
}

并查询:

repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(gender.M)));

repository.findAll(QUser.user.address.town.eq("Edinburgh"));

repository.findAll(QUser.user.forename.eq("Jim"));

其中QUser是queryDSL自动生成的类。

http://docs.spring.io/spring- data/jpa/docs/current/API/index.HTML?org/springframework/data/jpa/repository/support/queryDslRepositorySupport.HTML

http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html

从Spring Data模块的Gosling版本开始,现在支持从Web应用程序中的http参数自动生成谓词。

https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release- gosling#querydsl-web-support

解决方法

我有一个简单的REST服务,可通过Spring boot访问数据CrudRepository

该存储库已经实现了分页和排序功能,如下所示:

public interface FlightRepository extends CrudRepository<Flight,Long> {
  List<Flight> findAll(Pageable pageablE);
}

调用它:

Sort sort = new Sort(direction,ordering);
Pagerequest page = new Pagerequest(xoffset,xbase,sort);

return flightRepo.findAll(pagE);

我还想向此存储库添加过滤(例如,仅返回带有的实体id > 13 AND id < 27)。CrudRepository似乎不支持此功能。有什么方法可以实现这一目标,还是我需要使用其他方法?

感谢您的提示!

大佬总结

以上是大佬教程为你收集整理的使用Spring Boot CrudRepository过滤数据全部内容,希望文章能够帮你解决使用Spring Boot CrudRepository过滤数据所遇到的程序开发问题。

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

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