使用 Spring JPA 规范的多列搜索 [英] Multi-Column Search with Spring JPA Specifications

查看:26
本文介绍了使用 Spring JPA 规范的多列搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Spring-Boot 后端创建多字段搜索.如何使用 Specification 做到这一点?

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 {

步骤 2创建一个新类 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.

我需要为每一列设置一个方法吗?什么是 Root,什么是 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.

请参阅此处了解概述:

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>{

}

您还可以将请求参数自动绑定到控制器中的谓词:

You can also get request parameters automatically bound to a predicate in your Controller:

看这里:

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

所以你的控制器看起来像:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆