深入探索Spring Data JPA, 从Repository 到 Specifications 和 Querydsl
数据访问层,所谓的CRUD是后端程序员的必修课程,Spring Data JPA 可以让我们来简化CRUD过程,本文由简入深,从JPA的基本用法,到各种高级用法。
Repository
Spring Data JPA 可以用来简化data access的实现,借助JPA我们可以快速的实现一些简单的查询,分页,排序不在话下。
public interface MovieRepository extends JpaRepository<Movie, Long> {
List<Movie> findByTitle(String title, Sort sort);
Page<Movie> findByYear(Int year, Pageable pageable);
}
JPA会根据方法命名,通过JPA 查询生成器自动生成SQL,cool!
Criteria API
但是,简单并非万能,有时候也需要面对一些复杂的查询,不能享受JPA 查询生成器带来的便利。JPQ 提供了Criteria API
和
Criteria API
可以通过编程方式动态构建查询,强类型检查可以避免错误。核心原理就是构造一个Predicate
LocalDate today = new LocalDate();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Movie> query = builder.createQuery(Movie.class);
Root<Movie> root = query.from(Movie.class);
Predicate isComedy = builder.equal(root.get(Movie.genre), Genre.Comedy);
Predicate isReallyOld = builder.lessThan(root.get(Movie.createdAt), today.minusYears(25));
query.where(builder.and(isComedy, isReallyOld));
em.createQuery(query.select(root)).getResultList();
Predicate 可以很好的满足一些复杂的查询,但是他的问题在于不便于复用,因为你需要先构建CriteriaBuilder, CriteriaQuery
, Root
. 同时代码可读性也比较一般。
Specifications
能不能定义可复用的Predicate
呢? JPA 提供Specification
接口来解决这个问题。
先来看这个接口定义:
public interface Specification<T> {
Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb);
}
上文不是说需要先构建CriteriaBuilder, CriteriaQuery
, Root
吗,那么Specification接口就是给你提供这个三个参数,让你自己构建Predicate,想什么来什么。
我们用Specifications来改写代码,先定义Specification
public MovieSpecifications {
public static Specification<Movie> isComedy() {
return (root, query, cb) -> {
return cb.equal(root.get(Movie_.genre), Genre.Comedy);
};
}
public static Specification<Movie> isReallyOld() {
return (root, query, cb) -> {
return cb.lessThan(root.get(Movie_.createdAt), new LocalDate.now().minusYears(25));
};
}
}
然后改写MovieRepository
,为了让Repository可以运行Specification
,我们需要让其继承JpaSpecificationExecutor
接口。
public interface MovieRepository extends JpaRepository<Movie, Long>, JpaSpecificationExecutor<Movie> {
// query methods here
}
然后我们就可以愉快的使用定义好的Specification
了。
movieRepository.findAll(MovieSpecifications.isComedy());
movieRepository.findAll(MovieSpecifications.isReallyOld());
在这里,repository 的代理类,会自动准备好CriteriaBuilder, CriteriaQuery
, Root
,是不是很爽?
从面向对象编程来讲,MovieSpecifications
并不是很优雅,你可以这样做:
public MovieComedySpecification implements Specification<Movie> {
@Override
public Predicate toPredicate(Root<Movie> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Movie_.genre), Genre.Comedy);
}
联合Specifications
我们可以将多个predicates 合到一起使用,通过and,or来连接。
movieRepository.findAll(Specification.where(MovieSpecifications.isComedy())
.and(MovieSpecifications.isReallyOld()));
Specification 构造器
产品定义的业务逻辑,有时候会很复杂,比如我们需要根据条件动态拼接查询,我们可以定义一个SpecificationBuilder。
public enum SearchOperation {
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE;
public static final String[] SIMPLE_OPERATION_SET =
{ ":", "!", ">", "<", "~" };
public static SearchOperation getSimpleOperation(final char input)
{
switch (input) {
case ':': return EQUALITY;
case '!': return NEGATION;
case '>': return GREATER_THAN;
case '<': return LESS_THAN;
case '~': return LIKE;
default: return null;
}
}
}
public class SearchCriteria {
private String key;
private Object value;
private SearchOperation operation;
}
public final class MovieSpecificationsBuilder {
private final List<SearchCriteria> params;
public MovieSpecificationsBuilder() {
params = new ArrayList<>();
}
public Specification<Movie> build() {
// convert each of SearchCriteria params to Specification and construct combined specification based on custom rules
}
public final MovieSpecificationsBuilder with(final SearchCriteria criteria) {
params.add(criteria);
return this;
}
}
使用方法:
final MovieSpecificationsBuilder msb = new MovieSpecificationsBuilder();
// add SearchCriteria by invoking with()
final Specification<Movie> spec = msb.build();
movieRepository.findAll(spec);
Querydsl
Querydsl, 动态查询语言,支持JPA。先引入:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
Querydsl会根据表结构,生成meta-model
,需要引入APT插件
maven配置:
<project>
<build>
<plugins>
...
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</project>
假设,我们有下面的Domain类:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstname;
private String lastname;
// … methods omitted
}
在这里生成,会根据表结构生成查询classes,比如QCustomer
:
QCustomer customer = QCustomer.customer;
LocalDate today = new LocalDate();
BooleanExpression customerHasBirthday = customer.birthday.eq(today);
BooleanExpression isLongTermCustomer = customer.createdAt.lt(today.minusYears(2));
对比Specifications
,这里是BooleanExpression
,基本上基于生成的代码就可以构造了,更方便快捷。
现在我们到JPA使用,JPA 接口需要继承QueryDslPredicateExecutor
public interface CustomerRepository extends JpaRepository<Customer>, QueryDslPredicateExecutor {
// Your query methods here
}
查询代码:
BooleanExpression customerHasBirthday = customer.birthday.eq(today);
BooleanExpression isLongTermCustomer = customer.createdAt.lt(today.minusYears(2));
customerRepository.findAll(customerHasBirthday.and(isLongTermCustomer));
同样的,Queydsl 还有一些类似直接写SQL的骚操作。
简单如:
QCustomer customer = QCustomer.customer;
Customer bob = queryFactory.selectFrom(customer)
.where(customer.firstName.eq("Bob"))
.fetchOne();
多表查询:
QCustomer customer = QCustomer.customer;
QCompany company = QCompany.company;
query.from(customer, company);
多条件
queryFactory.selectFrom(customer)
.where(customer.firstName.eq("Bob"), customer.lastName.eq("Wilson"));
queryFactory.selectFrom(customer)
.where(customer.firstName.eq("Bob").and(customer.lastName.eq("Wilson")));
使用JOIN
QCat cat = QCat.cat;
QCat mate = new QCat("mate");
QCat kitten = new QCat("kitten");
queryFactory.selectFrom(cat)
.innerJoin(cat.mate, mate)
.leftJoin(cat.kittens, kitten)
.fetch();
对应JPQL
inner join cat.mate as mate
left outer join cat.kittens as kitten
另外一个例子
queryFactory.selectFrom(cat)
.leftJoin(cat.kittens, kitten)
.on(kitten.bodyWeight.gt(10.0))
.fetch();
JPQL version
select cat from Cat as cat
left join cat.kittens as kitten
on kitten.bodyWeight > 10.0
Ordering
QCustomer customer = QCustomer.customer;
queryFactory.selectFrom(customer)
.orderBy(customer.lastName.asc(), customer.firstName.desc())
.fetch();
Grouping
queryFactory.select(customer.lastName).from(customer)
.groupBy(customer.lastName)
.fetch();
子查询
QDepartment department = QDepartment.department;
QDepartment d = new QDepartment("d");
queryFactory.selectFrom(department)
.where(department.size.eq(
JPAExpressions.select(d.size.max()).from(d)))
.fetch();
小结
本文简单介绍了JPA的Repository,以及面向动态查询的Querydsl和Specifications 的用法,使用JPA可以有效减少代码编写量,提升代码易读性和可维护性。
参考
- https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
- http://www.querydsl.com/static/querydsl/latest/reference/html/ch02.html#jpa_integration
- https://medium.com/@milan.brankovic/spring-advanced-search-filtering-5ee850f9458c
作者:Jadepeng
出处:jqpeng的技术记事本--http://www.cnblogs.com/xiaoqi
您的支持是对博主最大的鼓励,感谢您的认真阅读。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
深入探索Spring Data JPA, 从Repository 到 Specifications 和 Querydsl的更多相关文章
- 扩展spring data jpa的repository
在我们编写代码的过程中,spring data jpa为我们的持久层提供的极大的方便,但有时spring data jpa提供的repository并不能完全满足我们开发的需求,因此就需要进行扩展.s ...
- IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA使用Repository<T, T>编程
1.环境搭建pom.xml搭建 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- 【spring data jpa】repository中使用@Query注解使用hql查询,使用@Param引用参数,报错:For queries with named parameters you need to use provide names for method parameters. Use @Param for query method parameters, or when on
在spring boot中, repository中使用@Query注解使用hql查询,使用@Param引用参数 如题报错: For queries with named parameters you ...
- 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...
- 快速搭建springmvc+spring data jpa工程
一.前言 这里简单讲述一下如何快速使用springmvc和spring data jpa搭建后台开发工程,并提供了一个简单的demo作为参考. 二.创建maven工程 http://www.cnblo ...
- spring data jpa hibernate jpa 三者之间的关系
JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架——因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...
- 转:spring data jpa、 hibernate、 jpa 三者之间的关系
原文链接:spring data jpa. hibernate. jpa 三者之间的关系 spring data jpa hibernate jpa 三者之间的关系 JPA规范与ORM框架之间的关系是 ...
- 学习Spring Data JPA
简介 Spring Data 是spring的一个子项目,在官网上是这样解释的: Spring Data 是为数据访问提供一种熟悉且一致的基于Spring的编程模型,同时仍然保留底层数据存储的特殊 ...
- spring data jpa、 hibernate、 jpa 三者之间的关系
http://www.cnblogs.com/xiaoheike/p/5150553.html JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架-- ...
随机推荐
- linux centos7使用docker安装elasticsearch,并且用Django连接使用
一:elasticsearch安装及配置 1:需求分析 当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果.这种需求依赖数据库的模糊查询like关键字可以实现,但是like关键字的效率极低,而 ...
- Burp时间到期之复活
Burp昨天到期了,找了好久终于找到可以用的了,分享给大家. https://pan.baidu.com/s/1hsEhUYS r6ls PS:https://github.com/mxcxvn/Bu ...
- C++ 异常处理 catch(...)介绍
转载:https://blog.csdn.net/fcsfcsfcs/article/details/77717567 catch(-)能够捕获多种数据类型的异常对象,所以它提供给程序员一种对异常 对 ...
- 《C++primerplus》第8章练习题
1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int).使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加 ...
- P3469 BLO-Blockade (缩点)
又可以水紫题了,好开心 前置芝士 无向图割点,然后脑子... 不会的童鞋,出门右转,百度百科...QAQ 首先,对于这道题,我们要求的是,割去每个点及他所连的边后,无向图中,有多少有序点对(\(x\) ...
- centos7 samba安装教程
samba的用途:有的时候,我们需要在centos7 的文件能共享给其他机器. rpm -qa|grep samba yum install -y samba setenforce 0 sed -i ...
- Signature Scanning(中文暂时译为"特征码扫描")是在C++(起码我是用C++^^)开发中很好的一种方式
1.介绍 本文主要简单介绍在没有代码的情况下,如何从一个动态链接库中获取某个函数的址.主要实现方式为Signature Scanning(特征码扫描) 2.什么是Signature Scanning( ...
- std::hash
std::hash 由于C++11引入了哈希表数据结构std::unordered_map和std::unordered_set,所以对于基本类型也实现了标准的哈希函数std::hash,标准并没有规 ...
- spring boot:使mybatis访问多个druid数据源(spring boot 2.3.2)
一,为什么要使用多个数据源? 1,什么情况下需要使用多个数据源? 当我们需要访问不同的数据库时,则需要配置配置多个数据源, 例如:电商的业务数据库(包括用户/商品/订单等) 和统 ...
- 用python和GDAL 读取GRIB数据
from osgeo import gdal import numpy as np path = './data/201912/anl_surf125.2019120100' dataset = gd ...