Skip to content

Commit fe1270e

Browse files
committed
更新 spring-boot-jpa-querydsl 进阶使用方式
1 parent 6166e6a commit fe1270e

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/model/UserModel.java

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.hibernate.annotations.GenericGenerator;
55

66
import javax.persistence.*;
7+
import java.util.Date;
78
import java.util.List;
89

910
/**
@@ -28,4 +29,6 @@ public class UserModel {
2829

2930
private int age;
3031

32+
private Date createDate;
33+
3134
}

spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/LessonService.java

+3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
* @Desc:
1212
*/
1313
public interface LessonService {
14+
1415
List<LessonModel> findLessonList(String name, Date startDate, String address, String userId) throws ParseException;
1516

1617
List<LessonModel> findLessonDynaList(String name, Date startDate, String address, String userId) throws ParseException;
18+
19+
List<LessonModel> findLessonSubqueryList(String name, String address);
1720
}

spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/UserService.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66
import java.util.List;
77

88
public interface UserService {
9-
// 更新
109
Long update(String id, String nickName);
1110

12-
// 删除
1311
Long delete(String id);
1412

15-
// 查询字段
1613
List<String> selectAllNameList();
1714

18-
// 查询实体
1915
List<UserModel> selectAllUserModelList();
2016

21-
// 查询实体并将结果封装至DTO
2217
List<UserDTO> selectAllUserDTOList();
2318

24-
// 去重查询
2519
List<String> selectDistinctNameList();
2620

27-
// 查询首个实体
2821
UserModel selectFirstUser();
2922

30-
// 查询单个实体,如果结果有多个,会抛`NonUniqueResultException`。
3123
UserModel selectUser(String id);
24+
25+
String mysqlFuncDemo(String id, String nickName, int age);
3226
}

spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/LessonServiceImpl.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.springboot.springbootjpaquerydsl.service.impl;
22

33
import com.querydsl.core.BooleanBuilder;
4+
import com.querydsl.jpa.JPAExpressions;
45
import com.querydsl.jpa.impl.JPAQueryFactory;
56
import com.springboot.springbootjpaquerydsl.model.LessonModel;
67
import com.springboot.springbootjpaquerydsl.model.QLessonModel;
@@ -24,13 +25,15 @@
2425
*/
2526
@Service
2627
public class LessonServiceImpl implements LessonService {
28+
2729
@Autowired
2830
JPAQueryFactory queryFactory;
2931

3032
@Override
3133
public List<LessonModel> findLessonList(String name, Date startDate, String address, String userId) throws ParseException {
3234
QLessonModel lessonModel = QLessonModel.lessonModel;
3335
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
36+
// 多条件查询示例
3437
return queryFactory.selectFrom(lessonModel)
3538
.where(
3639
lessonModel.name.like("%" + name + "%")
@@ -46,6 +49,7 @@ public List<LessonModel> findLessonDynaList(String name, Date startDate, String
4649
QLessonModel lessonModel = QLessonModel.lessonModel;
4750
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
4851

52+
// 动态查询示例
4953
BooleanBuilder builder = new BooleanBuilder();
5054

5155
if (!StringUtils.isEmpty(name)){
@@ -66,4 +70,18 @@ public List<LessonModel> findLessonDynaList(String name, Date startDate, String
6670

6771
return queryFactory.selectFrom(lessonModel).where(builder).fetch();
6872
}
73+
74+
@Override
75+
public List<LessonModel> findLessonSubqueryList(String name, String address) {
76+
QLessonModel lessonModel = QLessonModel.lessonModel;
77+
// 子查询示例,并无实际意义
78+
return queryFactory.selectFrom(lessonModel)
79+
.where(lessonModel.name.in(
80+
JPAExpressions
81+
.select(lessonModel.name)
82+
.from(lessonModel)
83+
.where(lessonModel.address.eq(address))
84+
))
85+
.fetch();
86+
}
6987
}

spring-boot-jpa-querydsl/src/main/java/com/springboot/springbootjpaquerydsl/service/impl/UserServiceImpl.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.springboot.springbootjpaquerydsl.service.impl;
22

33
import com.querydsl.core.types.Projections;
4+
import com.querydsl.core.types.dsl.Expressions;
45
import com.querydsl.jpa.impl.JPAQueryFactory;
56
import com.springboot.springbootjpaquerydsl.dto.UserDTO;
6-
import com.springboot.springbootjpaquerydsl.model.LessonModel;
77
import com.springboot.springbootjpaquerydsl.model.QLessonModel;
88
import com.springboot.springbootjpaquerydsl.model.QUserModel;
99
import com.springboot.springbootjpaquerydsl.model.UserModel;
@@ -30,31 +30,36 @@ public class UserServiceImpl implements UserService {
3030
@Override
3131
public Long update(String id, String nickName) {
3232
QUserModel userModel = QUserModel.userModel;
33+
// 更新
3334
return queryFactory.update(userModel).set(userModel.nickName, nickName).where(userModel.id.eq(id)).execute();
3435
}
3536

3637
@Override
3738
public Long delete(String id) {
3839
QUserModel userModel = QUserModel.userModel;
40+
// 删除
3941
return queryFactory.delete(userModel).where(userModel.id.eq(id)).execute();
4042
}
4143

4244
@Override
4345
public List<String> selectAllNameList() {
4446
QUserModel userModel = QUserModel.userModel;
47+
// 查询字段
4548
return queryFactory.select(userModel.nickName).from(userModel).fetch();
4649
}
4750

4851
@Override
4952
public List<UserModel> selectAllUserModelList() {
5053
QUserModel userModel = QUserModel.userModel;
54+
// 查询实体
5155
return queryFactory.selectFrom(userModel).fetch();
5256
}
5357

5458
@Override
5559
public List<UserDTO> selectAllUserDTOList() {
5660
QUserModel userModel = QUserModel.userModel;
5761
QLessonModel lessonModel = QLessonModel.lessonModel;
62+
// 连表查询实体并将结果封装至DTO
5863
return queryFactory
5964
.select(
6065
Projections.bean(UserDTO.class, userModel.nickName, userModel.age, lessonModel.startDate, lessonModel.address, lessonModel.name)
@@ -68,18 +73,46 @@ public List<UserDTO> selectAllUserDTOList() {
6873
@Override
6974
public List<String> selectDistinctNameList() {
7075
QUserModel userModel = QUserModel.userModel;
76+
// 去重查询
7177
return queryFactory.selectDistinct(userModel.nickName).from(userModel).fetch();
7278
}
7379

7480
@Override
7581
public UserModel selectFirstUser() {
7682
QUserModel userModel = QUserModel.userModel;
83+
// 查询首个实体
7784
return queryFactory.selectFrom(userModel).fetchFirst();
7885
}
7986

8087
@Override
8188
public UserModel selectUser(String id) {
8289
QUserModel userModel = QUserModel.userModel;
90+
// 查询单个实体,如果结果有多个,会抛`NonUniqueResultException`。
8391
return queryFactory.selectFrom(userModel).fetchOne();
8492
}
93+
94+
@Override
95+
public String mysqlFuncDemo(String id, String nickName, int age) {
96+
97+
QUserModel userModel = QUserModel.userModel;
98+
99+
// Mysql 聚合函数示例
100+
101+
// 聚合函数-avg()
102+
Double averageAge = queryFactory.select(userModel.age.avg()).from(userModel).fetchOne();
103+
104+
// 聚合函数-sum()
105+
Integer sumAge = queryFactory.select(userModel.age.sum()).from(userModel).fetchOne();
106+
107+
// 聚合函数-concat()
108+
String concat = queryFactory.select(userModel.nickName.concat(nickName)).from(userModel).fetchOne();
109+
110+
// 聚合函数-contains()
111+
Boolean contains = queryFactory.select(userModel.nickName.contains(nickName)).from(userModel).where(userModel.id.eq(id)).fetchOne();
112+
113+
// 聚合函数-DATE_FORMAT()
114+
String date = queryFactory.select(Expressions.stringTemplate("DATE_FORMAT({0},'%Y-%m-%d')", userModel.createDate)).from(userModel).fetchOne();
115+
116+
return null;
117+
}
85118
}

0 commit comments

Comments
 (0)