Spring Boot (六): 为 JPA 插上翅膀的 QueryDSL

在前面的文章中,我们介绍了 JPA 的基础使用方式,《Spring Boot (三): ORM 框架 JPA 与连接池 Hikari》,本篇文章,我们由入门至进阶的介绍一下为 JPA 插上翅膀的 QueryDSL。
1. 引言
不可否认的是 JPA 使用是非常方便的,极简化的配置,只需要使用注解,无需任何 xml 的配置文件,语义简单易懂,但是,以上的一切都建立在单表查询的前提下的,我们可以使用 JPA 默认提供的方法,简单加轻松的完成 CRUD 操作。
但是如果涉及到多表动态查询, JPA 的功能就显得有些捉襟见肘了,虽然我们可以使用注解 @Query ,在这个注解中写 SQL 或者 HQL 都是在拼接字符串,并且拼接后的字符串可读性非常的差,当然 JPA 还为我们提供了 Specification 来做这件事情,从我个人使用体验上来讲,可读性虽然还不错,但是在初学者上手的时候, Predicate 和 CriteriaBuilder 使用方式估计能劝退不少人,而且如果直接执行 SQL 连表查询,获得是一个 Object[] ,类型是什么?字段名是什么?这些都无法直观的获得,还需我们手动将 Object[] 映射到我们需要的 Model 类里面去,这种使用体验无疑是极其糟糕的。
这一切都在 QueryDSL 出世以后终结了, QueryDSL 语法与 SQL 非常相似,代码可读性非常强,异常简介优美,,并且与 JPA 高度集成,无需多余的配置,从笔者个人使用体验上来讲是非常棒的。可以这么说,只要会写 SQL ,基本上只需要看一下示例代码完全可以达到入门的级别。
2. QueryDSL 简介
QueryDSL 是一个非常活跃的开源项目,目前在 Github 上的发布的 Release 版本已经多达 251 个版本,目前最新版是 4.2.1 ,并且由 Querydsl Google组 和 StackOverflow 两个团队提供支持。
QueryDSL 是一个框架,可用于构造静态类型的类似SQL的查询。可以通过诸如 QueryDSL 之类的 API 构造查询,而不是将查询编写为内联字符串或将其外部化为XML文件。
例如,与简单字符串相比,使用 API 的好处是
IDE中的代码完成
几乎没有语法无效的查询
可以安全地引用域类型和属性
更好地重构域类型的更改
3. QueryDSL 使用实战
3.1 引入 Maven 依赖
代码清单:spring-boot-jpa-querydsl/pom.xml
<!--QueryDSL支持-->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<scope>provided</scope>
</dependency>
<!--QueryDSL支持-->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
- 这里无需指定版本号,已在
spring-boot-dependencies工程中定义。
3.2 添加 Maven 插件
添加这个插件是为了让程序自动生成 query type (查询实体,命名方式为:"Q"+对应实体名)。
上文引入的依赖中 querydsl-apt 即是为此插件服务的。
注:在使用过程中,如果遇到 query type 无法自动生成的情况,用maven更新一下项目即可解决(右键项目 -> Maven -> Update Folders)。
代码清单:spring-boot-jpa-querydsl/pom.xml
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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>
3.3 更新和删除
在 JPA 中已经为我们提供了非常简便的更新和删除的使用方式,我们完全没有必要使用 QueryDSL 的更新和删除,不过这里还是给出用法,供大家参考:
代码清单:spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/UserServiceImpl.java
@Override
public Long update(String id, String nickName) {
QUserModel userModel = QUserModel.userModel;
// 更新
return queryFactory.update(userModel).set(userModel.nickName, nickName).where(userModel.id.eq(id)).execute();
}
@Override
public Long delete(String id) {
QUserModel userModel = QUserModel.userModel;
// 删除
return queryFactory.delete(userModel).where(userModel.id.eq(id)).execute();
}
3.2 查询
QueryDSL 在查询这方面可以说玩的非常花了,比如一些有关 select() 和 fetch() 常用的写法如下:
代码清单:spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/UserServiceImpl.java
@Override
public List<String> selectAllNameList() {
QUserModel userModel = QUserModel.userModel;
// 查询字段
return queryFactory.select(userModel.nickName).from(userModel).fetch();
}
@Override
public List<UserModel> selectAllUserModelList() {
QUserModel userModel = QUserModel.userModel;
// 查询实体
return queryFactory.selectFrom(userModel).fetch();
}
@Override
public List<UserDTO> selectAllUserDTOList() {
QUserModel userModel = QUserModel.userModel;
QLessonModel lessonModel = QLessonModel.lessonModel;
// 连表查询实体并将结果封装至DTO
return queryFactory
.select(
Projections.bean(UserDTO.class, userModel.nickName, userModel.age, lessonModel.startDate, lessonModel.address, lessonModel.name)
)
.from(userModel)
.leftJoin(lessonModel)
.on(userModel.id.eq(lessonModel.userId))
.fetch();
}
@Override
public List<String> selectDistinctNameList() {
QUserModel userModel = QUserModel.userModel;
// 去重查询
return queryFactory.selectDistinct(userModel.nickName).from(userModel).fetch();
}
@Override
public UserModel selectFirstUser() {
QUserModel userModel = QUserModel.userModel;
// 查询首个实体
return queryFactory.selectFrom(userModel).fetchFirst();
}
@Override
public UserModel selectUser(String id) {
QUserModel userModel = QUserModel.userModel;
// 查询单个实体,如果结果有多个,会抛`NonUniqueResultException`。
return queryFactory.selectFrom(userModel).fetchOne();
}
3.4 复杂查询操作
上面列举了简单的查询,但实际我们会遇到相当复杂的操作,比如子查询,多条件查询,多表连查,使用示例如下:
代码清单:spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/LessonServiceImpl.java
@Service
public class LessonServiceImpl implements LessonService {
@Autowired
JPAQueryFactory queryFactory;
@Override
public List<LessonModel> findLessonList(String name, Date startDate, String address, String userId) throws ParseException {
QLessonModel lessonModel = QLessonModel.lessonModel;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 多条件查询示例
return queryFactory.selectFrom(lessonModel)
.where(
lessonModel.name.like("%" + name + "%")
.and(lessonModel.address.contains(address))
.and(lessonModel.userId.eq(userId))
.and(lessonModel.startDate.between(simpleDateFormat.parse("2018-12-31 00:00:00"), new Date()))
)
.fetch();
}
@Override
public List<LessonModel> findLessonDynaList(String name, Date startDate, String address, String userId) throws ParseException {
QLessonModel lessonModel = QLessonModel.lessonModel;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 动态查询示例
BooleanBuilder builder = new BooleanBuilder();
if (!StringUtils.isEmpty(name)){
builder.and(lessonModel.name.like("%" + name + "%"));
}
if (startDate != null) {
builder.and(lessonModel.startDate.between(simpleDateFormat.parse("2018-12-31 00:00:00"), new Date()));
}
if (!StringUtils.isEmpty(address)) {
builder.and(lessonModel.address.contains(address));
}
if (!StringUtils.isEmpty(userId)) {
builder.and(lessonModel.userId.eq(userId));
}
return queryFactory.selectFrom(lessonModel).where(builder).fetch();
}
@Override
public List<LessonModel> findLessonSubqueryList(String name, String address) {
QLessonModel lessonModel = QLessonModel.lessonModel;
// 子查询示例,并无实际意义
return queryFactory.selectFrom(lessonModel)
.where(lessonModel.name.in(
JPAExpressions
.select(lessonModel.name)
.from(lessonModel)
.where(lessonModel.address.eq(address))
))
.fetch();
}
}
3.5 Mysql 聚合函数
QueryDSL 已经内置了一些常用的 Mysql 的聚合函数,如果遇到 QueryDSL 没有提供的聚合函数也无需慌张, QueryDSL 为我们提供了 Expressions 这个类,我们可以使用这个类手动拼接一个就好,如下示例:
代码清单:spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/UserServiceImpl.java
@Override
public String mysqlFuncDemo(String id, String nickName, int age) {
QUserModel userModel = QUserModel.userModel;
// Mysql 聚合函数示例
// 聚合函数-avg()
Double averageAge = queryFactory.select(userModel.age.avg()).from(userModel).fetchOne();
// 聚合函数-sum()
Integer sumAge = queryFactory.select(userModel.age.sum()).from(userModel).fetchOne();
// 聚合函数-concat()
String concat = queryFactory.select(userModel.nickName.concat(nickName)).from(userModel).fetchOne();
// 聚合函数-contains()
Boolean contains = queryFactory.select(userModel.nickName.contains(nickName)).from(userModel).where(userModel.id.eq(id)).fetchOne();
// 聚合函数-DATE_FORMAT()
String date = queryFactory.select(Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')", userModel.createDate)).from(userModel).fetchOne();
return null;
}
4. 小结
有关 QueryDSL 的介绍到这里就结束了,不知道各位读者看了上面的示例,有没有一种直接读 SQL 的感觉,而且这种 SQL 还是使用 OOM 的思想,将原本 Hibernate 没有做好的事情给出了一个相当完美的解决方案,上手简单易操作,而又无需写 SQL ,实际上我们操作的还是对象类。
5. 示例代码
6. 参考
Spring Boot (六): 为 JPA 插上翅膀的 QueryDSL的更多相关文章
- Spring Boot(六):如何使用mybatis
Spring Boot(六):如何使用mybatis orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动 ...
- 基于Spring Boot,使用JPA动态调用Sql查询数据
在<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>,<基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合 ...
- 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合
在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...
- 初识在Spring Boot中使用JPA
前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...
- 【实验一 】Spring Boot 集成 hibernate & JPA
转眼间,2018年的十二分之一都快过完了,忙于各类事情,博客也都快一个月没更新了.今天我们继续来学习Springboot对象持久化. 首先JPA是Java持久化API,定义了一系列对象持久化的标准,而 ...
- spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
前言: 上传下载功能算是一个非常常见的功能,如果使用MultipartHttpServletRequest来做上传功能. 不配置上传大小的话,默认是2M.在有些场景,这个肯定不能满足条件. 上传代码: ...
- 插上翅膀,让Excel飞起来——xlwings(二)
在上一篇插上翅膀,让Excel飞起来——xlwings(一)中提到利用xlwings模块,用python操作Excel有如下的优点: xlwings能够非常方便的读写Excel文件中的数据,并且能够进 ...
- 使用spring boot中的JPA操作数据库
前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...
- 从零开始的Spring Boot(3、Spring Boot静态资源和文件上传)
Spring Boot静态资源和文件上传 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式) https://www. ...
随机推荐
- [系列] go-gin-api 规划目录和参数验证(二)
目录 概述 规划目录结构 模型绑定和验证 自定义验证器 制定 API 返回结构 源码地址 go-gin-api 系列文章 概述 首先同步下项目概况: 上篇文章分享了,使用 go modules 初始化 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- 第一篇 特征提取以及openvslam中的相关实现详解
参考尺度空间理论 金字塔 当用一个机器视觉系统分析未知场景时,计算机没有办法预先知道图像中物体尺度,因此,我们需要同时考虑图像在多尺度下的描述,获知感兴趣物体的最佳尺度.所以在很多时候,我们会在将图像 ...
- Lambada和linq查询数据库的比较
1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq: from s in Student ...
- HibernateSynchronizer的安装与使用
HibernateSynchronizer的作用是自动生成hibernate配置文件,即hibernate.cfg.xml文件,映射文件,Plain Object类文件和一些基础数据库操作文件. 安装 ...
- [python]python字典
1.简介 字典是python中的映射数据类型,由‘键-值’(key-value)对构成. 键:几乎所有类型的python对象都可以用作键,不过一般还是以数字或者字符串最为常用. 值:可以是任意类型的p ...
- P2050 [NOI2012]美食节 动态连边优化费用流
题意 类似的一道排队等候,算最小总等待时间的题目. 思路 但是这道题的边数很多,直接跑会tle,可以动态加边,就是先连上倒数第一次操作的边,跑一遍费用流,然后对使用了倒数第一条边的点,连上相应的倒数第 ...
- hdu6351 Beautiful Now 杭电第五场 暴力枚举
Beautiful Now Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- 牛客第五场 G max 思维
链接:https://www.nowcoder.com/acm/contest/143/G来源:牛客网 Give two positive integer c, n. You need to find ...
- Matlab2016b破解安装教程——超详细
一.MATLAB是什么 MATLAB :是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simuli ...