本文主要是介绍Spring Jpa-spec插件介绍,构建复杂查询,快速上手,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载自:https://baijiahao.baidu.com/s?id=1627039521629851707&wfr=spider&for=pc
1.添加Maven依赖
<dependency> <groupId>com.github.wenhao</groupId> <artifactId>jpa-spec</artifactId> <version>3.2.4</version><!-- 把所有依赖都过滤 --><exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions>
</dependency>
我们创建Repository 的时候需要继承2个JpaRepository 和JpaSpecificationExecutor
Equal/NotEqual 实例
find any person nickName equals to "dog" and name equals to "Jack"/"Eric" or null value, and company is null.
In/NotIn 实例
find any person name in "Jack" or "Eric" and company not in "ThoughtWorks" or "IBM".
Comparison 实例
Support any comparison class which implements Comparable interface, find any people age bigger than 18.
Between 实例
Like/NotLike 实例
find any person name like %ac% or %og%, company not like %ec%.
And and Or实例
Join关联查询
多对一查询
多对多查询
自定义查询:
多对一查询
多对多查询
排序查询:
分页查询
@Testpublic void testPrint(){Specification<Student> grade = Specifications.<Student>and().between("grade", 1, 20).build();// 默认为升序排列Sort sort = new Sort(Sort.Direction.DESC,"grade");// 分页条件中加入排序PageRequest pageRequest = PageRequest.of(1 - 1, 10,sort);// 根据条件、分页、排序来查找Page<Student> all = studentRepository.findAll(grade, pageRequest);// 查找所有并排序List<Student> all1 = studentRepository.findAll(sort);// 根据条件查找,并排序List<Student> all2 = studentRepository.findAll(grade, sort);// 根据分页条件来查询Page<Student> all3 = studentRepository.findAll(pageRequest);List<Student> content = all.getContent();for (Student student : content) {System.out.println(student);}}
这篇关于Spring Jpa-spec插件介绍,构建复杂查询,快速上手的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!