使用Spring JPA规范进行多列搜索 [英] Multi-Column Search with Spring JPA Specifications
问题描述
我想在Spring-Boot后端中创建一个多字段搜索.如何使用Specification<T>
做到这一点?
I want to create a multi field search in a Spring-Boot back-end. How to do this with a Specification<T>
?
环境
Springboot
Hibernate
Gradle
Intellij
前端的UI是Jquery Datatable.每列允许应用一个字符串搜索词. 将一栏以上的搜索字词连在一起.
The UI in the front end is a Jquery Datatable. Each column allows a single string search term to be applied. The search terms across more than one column is joined by a and
.
我已经将来自前端的过滤器填充到Java对象中.
I have the filters coming from the front end already getting populated into a Java object.
第1步 扩展JPA规范执行器
Step 1 Extend JPA Specification executor
public interface SomeRepository extends JpaRepository<Some, Long>, PagingAndSortingRepository<Some, Long>, JpaSpecificationExecutor {
第二步 创建一个新类SomeSpec
Step2 Create a new class SomeSpec
在这里,我迷失了代码的外观以及它的工作方式.
This is where I am lost as to what the code looks like it and how it works.
每个列都需要一个方法吗? 什么是根,什么是Criteria Builder? 还需要什么?
Do I need a method for each column? What is Root and what is Criteria Builder? What else is required?
我在JPA还是个新手,所以虽然我不需要任何人为我编写代码,但详细的解释会很好.
I am rather new at JPA so while I don't need anyone to write the code for me a detailed explanation would be good.
更新 看来QueryDSL是解决此问题的更简单更好的方法.我正在使用Gradle.我是否需要从
UPDATE It appears QueryDSL is the easier and better way to approach this. I am using Gradle. Do I need to change my build.gradle from this ?
推荐答案
您可以考虑使用Spring Data对QueryDSL的支持,因为无需编写大量代码即可获得很多好处,即您实际上不必编写规范
You could consider using Spring Data's support for QueryDSL as you would get quite a lot without having to write very much code i.e. you would not actually have to write the specifictions.
请参阅此处以获取概述:
See here for an overview:
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
尽管这种方法确实很方便(您甚至不必 编写一行实现代码以获取查询 执行)有两个缺点:首先,查询方法数量众多 可能会因为以下原因而在更大的应用程序中增长-这是第二个 点-查询定义了一组固定的条件.为了避免这些 有两个缺点,如果您能提出一套 您可以动态组合以建立自己的原子谓词 查询?
Although this approach is really convenient (you don’t even have to write a single line of implementation code to get the queries executed) it has two drawbacks: first, the number of query methods might grow for larger applications because of - and that’s the second point - the queries define a fixed set of criterias. To avoid these two drawbacks, wouldn’t it be cool if you could come up with a set of atomic predicates that you could combine dynamically to build your query?
因此,您的存储库实质上就是:
So essentially your repository becomes:
public interface SomeRepository extends JpaRepository<Some, Long>,
PagingAndSortingRepository<Some, Long>, QueryDslPredicateExecutor<Some>{
}
您还可以在Controller中获得自动绑定到谓词的请求参数:
You can also get request parameters automatically bound to a predicate in your Controller:
在这里看到:
SO your Controller would look like:
@Controller
class SomeController {
private final SomeRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model,
@QuerydslPredicate(root = Some.class) Predicate predicate,
Pageable pageable) {
model.addAttribute("data", repository.findAll(predicate, pageable));
return "index";
}
}
因此,有了上述内容,这仅仅是在项目上启用QueryDSL的一种情况,并且UI现在应该能够通过各种条件组合来过滤,排序和分页数据.
So with the above in place it is simply a Case of enabling QueryDSL on your project and the UI should now be able to filter, sort and page data by various combinations of criteria.
这篇关于使用Spring JPA规范进行多列搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!