Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
usedCarAuction
/
aus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 7360189b
authored
Feb 10, 2026
by
杨卫
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:query dsl 工具类提取
1 parent
fc1ed3eb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
173 additions
and
17 deletions
aus-core/src/main/java/com/glsx/usedcar/module/inspect/service/InspectorService.java
aus-core/src/main/java/com/glsx/usedcar/util/QuerydslPageHelper.java
aus-core/src/main/java/com/glsx/usedcar/module/inspect/service/InspectorService.java
View file @
7360189
...
...
@@ -13,6 +13,7 @@ import com.glsx.usedcar.module.inspect.vo.InspectorAddVO;
import
com.glsx.usedcar.module.inspect.vo.InspectorQryVO
;
import
com.glsx.usedcar.module.inspect.vo.InspectorRespVO
;
import
com.glsx.usedcar.module.inspect.vo.InspectorUpdateVO
;
import
com.glsx.usedcar.util.QuerydslPageHelper
;
import
com.google.common.collect.Sets
;
import
com.querydsl.core.BooleanBuilder
;
import
com.querydsl.core.types.Projections
;
...
...
@@ -20,7 +21,6 @@ import com.querydsl.jpa.impl.JPAQueryFactory;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.stereotype.Service
;
...
...
@@ -134,11 +134,27 @@ public class InspectorService {
*/
public
Page
<
InspectorRespVO
>
pageInspectors
(
Pagination
<
InspectorQryVO
>
pagination
)
{
return
QuerydslPageHelper
.
executePageQuery
(
pagination
,
this
::
buildInspectorPredicate
,
this
::
executeCountQuery
,
this
::
executeContentQuery
,
this
::
buildPageRequest
);
}
/**
* 查询条件构建函数
* @author yangwei
* @since 2026/2/10 17:55
* @param pagination 分页参数以及查询条件
* @return com.querydsl.core.BooleanBuilder
*/
private
BooleanBuilder
buildInspectorPredicate
(
Pagination
<
InspectorQryVO
>
pagination
)
{
QInspector
qInspector
=
QInspector
.
inspector
;
BooleanBuilder
predicate
=
new
BooleanBuilder
();
predicate
.
and
(
qInspector
.
deleteFlag
.
eq
(
DeleteFlag
.
NOT_DELETED
.
getCode
()));
// 添加过滤条件
Set
<
String
>
queryParameters
=
Sets
.
newHashSet
(
JpaQueryConstants
.
Inspector
.
KEYWORD
);
Optional
.
ofNullable
(
pagination
.
getFilters
())
.
filter
(
CollUtil:
:
isNotEmpty
)
...
...
@@ -150,39 +166,67 @@ public class InspectorService {
if
(
JpaQueryConstants
.
Inspector
.
KEYWORD
.
equals
(
field
))
{
Optional
.
ofNullable
(
filter
.
getValue
())
.
filter
(
key
->
StrUtil
.
isNotEmpty
((
String
)
key
))
.
ifPresent
(
value
->
{
predicate
.
and
(
qInspector
.
name
.
like
(
SymbolConsts
.
PERCENT
+
value
+
SymbolConsts
.
PERCENT
)
.
or
(
qInspector
.
phone
.
like
(
SymbolConsts
.
PERCENT
+
value
+
SymbolConsts
.
PERCENT
)));
});
.
map
(
value
->
SymbolConsts
.
PERCENT
+
value
+
SymbolConsts
.
PERCENT
)
.
ifPresent
(
value
->
predicate
.
and
(
qInspector
.
name
.
like
(
value
)
.
or
(
qInspector
.
phone
.
like
(
value
))));
}
});
long
total
=
Optional
.
ofNullable
(
jpaQueryFactory
.
select
(
qInspector
.
count
())
return
predicate
;
}
/**
* 总数查询函数
* @author yangwei
* @since 2026/2/10 17:56
* @param predicate 查询条件
* @return java.lang.Long
*/
private
Long
executeCountQuery
(
BooleanBuilder
predicate
)
{
QInspector
qInspector
=
QInspector
.
inspector
;
return
Optional
.
ofNullable
(
jpaQueryFactory
.
select
(
qInspector
.
count
())
.
from
(
qInspector
)
.
where
(
predicate
)
.
fetchOne
()).
orElse
(
0L
);
int
page
=
(
pagination
.
getOffset
()
+
pagination
.
getLimit
()
-
1
)
/
pagination
.
getLimit
();
// 查询数据列表
List
<
InspectorRespVO
>
content
=
jpaQueryFactory
.
select
(
}
/**
* 数据查询函数
* @author yangwei
* @since 2026/2/10 17:57
* @param predicate 查询条件
* @param pagination 分页参数
* @return java.util.List<com.glsx.usedcar.module.inspect.vo.InspectorRespVO>
*/
private
List
<
InspectorRespVO
>
executeContentQuery
(
BooleanBuilder
predicate
,
Pagination
<
InspectorQryVO
>
pagination
)
{
QInspector
qInspector
=
QInspector
.
inspector
;
return
jpaQueryFactory
.
select
(
Projections
.
constructor
(
InspectorRespVO
.
class
,
qInspector
.
id
,
qInspector
.
name
,
qInspector
.
password
,
qInspector
.
phone
,
qInspector
.
status
)
)
.
from
(
qInspector
)
)
)
.
from
(
qInspector
)
.
where
(
predicate
)
.
offset
(
pagination
.
getOffset
())
.
limit
(
pagination
.
getLimit
())
.
orderBy
(
qInspector
.
updateTime
.
desc
(),
qInspector
.
id
.
desc
())
.
fetch
();
}
// todo 查询当前任务和本月单量
return
new
PageImpl
<>(
content
,
PageRequest
.
of
(
page
,
pagination
.
getLimit
(),
Sort
.
by
(
Sort
.
Order
.
desc
(
JpaQueryConstants
.
Inspector
.
UPDATE_TIME
))),
total
);
/**
* 分页请求构建函数
* @author yangwei
* @since 2026/2/10 17:58
* @param baseRequest 基础分页请求
* @return org.springframework.data.domain.PageRequest
*/
private
PageRequest
buildPageRequest
(
PageRequest
baseRequest
)
{
return
baseRequest
.
withSort
(
Sort
.
by
(
Sort
.
Order
.
desc
(
JpaQueryConstants
.
Inspector
.
UPDATE_TIME
)));
}
}
aus-core/src/main/java/com/glsx/usedcar/util/QuerydslPageHelper.java
0 → 100644
View file @
7360189
package
com
.
glsx
.
usedcar
.
util
;
import
com.querydsl.core.BooleanBuilder
;
import
com.querydsl.core.types.dsl.EntityPathBase
;
import
com.querydsl.jpa.impl.JPAQueryFactory
;
import
lombok.experimental.UtilityClass
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageImpl
;
import
org.springframework.data.domain.PageRequest
;
import
org.summerframework.orm.paging.Pagination
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.function.BiFunction
;
import
java.util.function.Function
;
/**
* querydsl 分页查询工具类
* @author yangwei
* @version 1.0
* @since 2026/02/10 17:41
*/
@UtilityClass
public
class
QuerydslPageHelper
{
/**
* 通用分页查询模板
*
* @param pagination 分页参数
* @param predicateBuilder 查询条件构建函数
* @param countQuery 总数查询函数
* @param contentQuery 数据查询函数
* @param pageRequestBuilder 分页请求构建函数
* @return 分页结果
*/
public
<
T
,
R
>
Page
<
R
>
executePageQuery
(
Pagination
<
T
>
pagination
,
Function
<
Pagination
<
T
>,
BooleanBuilder
>
predicateBuilder
,
Function
<
BooleanBuilder
,
Long
>
countQuery
,
BiFunction
<
BooleanBuilder
,
Pagination
<
T
>,
List
<
R
>>
contentQuery
,
Function
<
PageRequest
,
PageRequest
>
pageRequestBuilder
)
{
// 1. 构建查询条件
BooleanBuilder
predicate
=
predicateBuilder
.
apply
(
pagination
);
// 2. 查询总数(使用Optional避免NPE)
long
total
=
Optional
.
ofNullable
(
countQuery
.
apply
(
predicate
)).
orElse
(
0L
);
if
(
total
==
0
)
{
return
new
PageImpl
<>(
Collections
.
emptyList
(),
buildPageRequest
(
pagination
,
pageRequestBuilder
),
0L
);
}
// 3. 查询数据列表
List
<
R
>
content
=
contentQuery
.
apply
(
predicate
,
pagination
);
// 4. 构建分页结果
return
new
PageImpl
<>(
content
,
buildPageRequest
(
pagination
,
pageRequestBuilder
),
total
);
}
/**
* 构建分页请求
*
* @param pagination 分页参数
* @param pageRequestBuilder 分页请求构建函数
* @return PageRequest
*/
private
<
T
>
PageRequest
buildPageRequest
(
Pagination
<
T
>
pagination
,
Function
<
PageRequest
,
PageRequest
>
pageRequestBuilder
)
{
int
page
=
(
pagination
.
getOffset
()
+
pagination
.
getLimit
()
-
1
)
/
pagination
.
getLimit
();
PageRequest
baseRequest
=
PageRequest
.
of
(
page
,
pagination
.
getLimit
());
return
pageRequestBuilder
!=
null
?
pageRequestBuilder
.
apply
(
baseRequest
)
:
baseRequest
;
}
/**
* 简化版 - 适用于标准分页查询
* @author yangwei
* @since 2026/2/10 17:55
* @param pagination 分页参数
* @param predicateBuilder 查询条件构建函数
* @param contentQuery 数据查询函数
* @param queryFactory 查询工厂
* @param entityPath 实体路径
* @return org.springframework.data.domain.Page<R>
*/
public
static
<
T
,
R
>
Page
<
R
>
simplePageQuery
(
Pagination
<
T
>
pagination
,
Function
<
Pagination
<
T
>,
BooleanBuilder
>
predicateBuilder
,
BiFunction
<
BooleanBuilder
,
Pagination
<
T
>,
List
<
R
>>
contentQuery
,
JPAQueryFactory
queryFactory
,
EntityPathBase
<?>
entityPath
)
{
return
executePageQuery
(
pagination
,
predicateBuilder
,
predicate
->
Optional
.
ofNullable
(
queryFactory
.
select
(
entityPath
.
count
())
.
from
(
entityPath
)
.
where
(
predicate
)
.
fetchOne
()
).
orElse
(
0L
),
contentQuery
,
null
);
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment