Spring Data JPA进阶——Specifications和Querydsl

本篇介绍一下spring Data JPA中能为数据访问程序的开发带来更多便利的特性,我们知道,Spring Data repository的配置很简单,一个典型的repository像下面这样:

  1. public interface CustomerRepository extends JpaRepository<Customer, Long> {
  2. Customer findByEmailAddress(String emailAddress);
  3. List<Customer> findByLastname(String lastname, Sort sort);
  4. Page<Customer> findByFirstname(String firstname, Pageable pageable);
  5. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第一个方法表示根据email查询一个Customer,第二个方法表示根据lastName和排序条件查询一个Customer的集合,第三个方法表示根据fristName和分页的信息查询一页Customer

这样的方式非常简单,甚至不用编写方法的实现就可以实现查询的功能,但是这仍然有个弊端,如果查询条件增长,方法会越来越多,如果能动态的组装查询条件就好了

那么,可以吗?答案当然是yes

我们都知道JPA提供了Criteria API,下面我们就用一个例子,展示一下Criteria的使用,想象这样一个场景,我们想针对长期客户,在生日那天给他发一段祝福,我们怎么做呢?

使用Criteria API

我们有两个条件,生日和长期客户,我们假设两年前注册的就是长期客户吧,怎么用JPA 2.0的Criteria API实现呢:

  1. LocalDate today = new LocalDate();
  2. CriteriaBuilder builder = em.getCriteriaBuilder();
  3. CriteriaQuery<Customer> query = builder.createQuery(Customer.class);
  4. Root<Customer> root = query.from(Customer.class);
  5. Predicate hasBirthday = builder.equal(root.get(Customer_.birthday), today);
  6. Predicate isLongTermCustomer = builder.lessThan(root.get(Customer_.createdAt), today.minusYears(2);
  7. query.where(builder.and(hasBirthday, isLongTermCustomer));
  8. em.createQuery(query.select(root)).getResultList();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

我们先创建了一个LocalDate对象,然后是三行样板代码啊,后面两行是建立查询条件,然后通过where子句连在一起,然后执行查询

上面查询有两个问题

  • 第一,由于每次要先建立CriteriaBuilder,CriteriaQuery,Root,所以导致查询条件的重用和扩展性不是很好
  • 第二,上面程序可读性一般,并不能一目了然知道程序在干嘛

使用Specifications

为了重用查询条件,我们引入了Specification接口,这是从Eric Evans’ Domain Driven Design 一书中的概念衍生出来的,它为对一个实体查询的谓词定义了一个规范,实体类型由Specification接口的泛型参数来决定,这个接口只包含下面一个方法:

  1. public interface Specification<T> {
  2. Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb);
  3. }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

我们现在可以通过一个工具类很容易的使用它:

  1. public CustomerSpecifications {
  2. public static Specification<Customer> customerHasBirthday() {
  3. return new Specification<Customer> {
  4. public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
  5. return cb.equal(root.get(Customer_.birthday), today);
  6. }
  7. };
  8. }
  9. public static Specification<Customer> isLongTermCustomer() {
  10. return new Specification<Customer> {
  11. public Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb) {
  12. return cb.lessThan(root.get(Customer_.createdAt), new LocalDate.minusYears(2));
  13. }
  14. };
  15. }
  16. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

诚然,这并不是最优雅的代码,但是至少很好地解决了我们重用判定条件的需求,如何执行呢,很简单,我们只要让repository继承JpaSpecificationExecutor接口即可:

  1. public interface CustomerRepository extends JpaRepository<Customer>, JpaSpecificationExecutor {
  2. // Your query methods here
  3. }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

然后可以像下面这样调用:

  1. customerRepository.findAll(hasBirthday());
  2. customerRepository.findAll(isLongTermCustomer());
  • 1
  • 2
  • 1
  • 2

默认实现会为你提供CriteriaQuery,Root,CriteriaBuilder等对象,通过给定的Specification应用判定条件,然后执行查询,这样的好处就是我们可以随意组合查询条件,而不用写很多个方法,Specifications工具类提供了一写遍历方法来组合条件,例如and(…)、or(…)等连接方法,还有where(…)提供了更易读的表达形式,下面我们看一下效果:

  1. customerRepository.findAll(where(customerHasBirthday()).and(isLongTermCustomer()));
  • 1
  • 1

相比JPA Criteria API的原生接口,我们的实现更加具有扩展性和可读性,当时实现Specification的时候需要一点小波折,但这是值得的

使用Querydsl

为了解决上述的痛苦,一个叫Querydsl的开源项目也提供了类似的解决方案,但是实现有所不同,提供了更有好的API,而且不仅支持JPA,还支持hibernate,JDO,Lucene,JDBC甚至是原始集合的查询

为了使用Querydsl,需要在pom.xml中引入依赖并且配置一个额外的APT插件

  1. <plugin>
  2. <groupId>com.mysema.maven</groupId>
  3. <artifactId>maven-apt-plugin</artifactId>
  4. <version>1.0</version>
  5. <executions>
  6. <execution>
  7. <phase>generate-sources</phase>
  8. <goals>
  9. <goal>process</goal>
  10. </goals>
  11. <configuration>
  12. <outputDirectory>target/generated-sources</outputDirectory>
  13. <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
  14. </configuration>
  15. </execution>
  16. </executions>
  17. </plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

下面就可以通过QCustomer来实现我们上述的功能了

  1. QCustomer customer = QCustomer.customer;
  2. LocalDate today = new LocalDate();
  3. BooleanExpression customerHasBirthday = customer.birthday.eq(today);
  4. BooleanExpression isLongTermCustomer = customer.createdAt.lt(today.minusYears(2));
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

上面的写法不仅读来很顺畅,BooleanExpressions还可以直接重用,免去使用更多包装方法的写法,更酷的是还可以得到IDE代码自动完成的支持,要执行查询,跟Specification类似,让repository继承QueryDslPredicateExecutor接口即可:

  1. public interface CustomerRepository extends JpaRepository<Customer>, QueryDslPredicateExecutor {
  2. // Your query methods here
  3. }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

可以通过下面的方式调用

  1. BooleanExpression customerHasBirthday = customer.birthday.eq(today);
  2. BooleanExpression isLongTermCustomer = customer.createdAt.lt(today.minusYears(2));
  3. customerRepository.findAll(customerHasBirthday.and(isLongTermCustomer));
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

总结

Spring Data JPA repository抽象允许通过把JPA Criteria API包装到Specification中来简化开发,还可以使用Querydsl,实现方法也很简单,分别集成JpaSpecificationExecutor或者QueryDslPredicateExecutor即可,当然,如果需要的话,一起使用也没问题

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

Spring Data JPA进阶——Specifications和Querydsl的更多相关文章

  1. Spring Data JPA 的 Specifications动态查询

    主要的结构: 有时我们在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecutor接口查询. ...

  2. Spring Data JPA 进阶

    Java持久化查询语言概述 Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起,使用这种语言编写的查询是可移植的,可以被编 ...

  3. 一文搞定 Spring Data JPA

    Spring Data JPA 是在 JPA 规范的基础上进行进一步封装的产物,和之前的 JDBC.slf4j 这些一样,只定义了一系列的接口.具体在使用的过程中,一般接入的是 Hibernate 的 ...

  4. 深入探索Spring Data JPA, 从Repository 到 Specifications 和 Querydsl

    数据访问层,所谓的CRUD是后端程序员的必修课程,Spring Data JPA 可以让我们来简化CRUD过程,本文由简入深,从JPA的基本用法,到各种高级用法. Repository Spring ...

  5. Spring Data JPA教程, 第五部分: Querydsl(未翻译)

    The fourth part of my Spring Data JPA tutorialdescribed how you can implement more advanced queries ...

  6. 如何在Spring Data JPA中引入Querydsl

    一.环境说明 基础框架采用Spring Boot.Spring Data JPA.Hibernate.在动态查询中,有一种方式是采用Querydsl的方式. 二.具体配置 1.在pom.xml中,引入 ...

  7. Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍

    大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...

  8. Spring data JPA中使用Specifications动态构建查询

    有时我们在查询某个实体的时候,给定的条件是不固定的,这是我们就需要动态 构建相应的查询语句,在JPA2.0中我们可以通过Criteria接口查询,JPA criteria查询.相比JPQL,其优势是类 ...

  9. Spring Data JPA教程, 第四部分: JPA Criteria Queries(未翻译)

    The third part of my Spring Data JPA tutorialdescribed how you can create custom queries by using qu ...

随机推荐

  1. nginx配置图片防盗链

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 30d; access_log off; valid_referers none blocked ...

  2. C#根据时间产生有序的GUID编码

    public static Guid GenerateGuid() { byte[] guidArray = Guid.NewGuid().ToByteArray(); , , ); DateTime ...

  3. Python基础之--常用模块

    Python 模块 为了实现对程序特定功能的调用和存储,人们将代码封装起来,可以供其他程序调用,可以称之为模块. 如:os 是系统相关的模块:file是文件操作相关的模块:sys是访问python解释 ...

  4. JS获取当前对象大小以及屏幕分辨率等...

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta nam ...

  5. <Web 之困 现代Web应用安全指南>一本好书 69.00?

    NET代码安全 界面漏洞防范与程序优化 一. SQL 注入攻击的源头 1. 过滤或转移危险字符 2.  使用SqlParameter类:.NET 框架有一个叫做SqlParameter 的集合类型,可 ...

  6. XMLHttpRequestUtil

    //XMLHttpRequest类 function XMLHTTPRequestUtil() { this.Instance = null; this.GetXMLHttpRequest = fun ...

  7. 正确运用synchronized和二次判断 实现多线程安全

    正确运用synchronized和二次判断 实现多线程安全,做出高效二符合预期的程序,特别是多个线程跑一个对象的时候,如下图所示:  测试代码如下: 特别注意if(shutdownRequested) ...

  8. SVN Tree Conflict 的分析

    所谓Tree Confict,就是至少有一个人修改了目录结构,包括文件或者文件所在目录的改名.删除.移动.然后Update或Merge的时候就报了Tree Conflict. 介绍一下概念Delete ...

  9. Linux服务器管理: 系统的进程管理top命令

    查看系统运行状态的命令top [root@localhost~]#top [选项] 选项: -d 秒数 指定top命令每个几秒更新.默认为3秒 在top命令的交互模式当中可以执行的命令 ?或h 查看帮 ...

  10. Eclipse中使用tomcat 8服务器初级教程

    Eclipse中使用tomcat容器时,经常遇到的问题是启动不成功,输入localhost:8080报404,本文就是教大家破解这个问题.(不过这是很初级的问题了,大牛勿喷) 步骤 1 Window- ...