Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-backend
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
cloud-backend
Commits
8cbff959
Commit
8cbff959
authored
Jun 19, 2024
by
袁伟铭
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化代码
parent
7140639a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
10 deletions
+33
-10
xxx-common-utils/src/main/java/com/zq/common/utils/PagingUtils.java
+33
-10
No files found.
xxx-common-utils/src/main/java/com/zq/common/utils/PagingUtils.java
View file @
8cbff959
package
com
.
zq
.
common
.
utils
;
package
com
.
zq
.
common
.
utils
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.bean.copier.CopyOptions
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
com.baomidou.mybatisplus.core.conditions.ISqlSegment
;
import
com.baomidou.mybatisplus.core.conditions.ISqlSegment
;
import
com.baomidou.mybatisplus.core.conditions.Wrapper
;
import
com.baomidou.mybatisplus.core.conditions.Wrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
...
@@ -16,6 +19,7 @@ import com.zq.common.exception.BusinessException;
...
@@ -16,6 +19,7 @@ import com.zq.common.exception.BusinessException;
import
com.zq.common.vo.PageReqVo
;
import
com.zq.common.vo.PageReqVo
;
import
com.zq.common.vo.PageVo
;
import
com.zq.common.vo.PageVo
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.function.Function
;
import
java.util.function.Function
;
...
@@ -147,16 +151,18 @@ public abstract class PagingUtils {
...
@@ -147,16 +151,18 @@ public abstract class PagingUtils {
* @return
* @return
*/
*/
public
static
<
R
,
Q
extends
PageReqVo
>
PageVo
<
R
>
paging
(
Q
reqVo
,
BaseMapper
<
R
>
mapper
,
Class
<
R
>
clazz
,
LambdaQueryWrapper
<
R
>
lambdaQuery
,
boolean
searchCount
)
{
public
static
<
R
,
Q
extends
PageReqVo
>
PageVo
<
R
>
paging
(
Q
reqVo
,
BaseMapper
<
R
>
mapper
,
Class
<
R
>
clazz
,
LambdaQueryWrapper
<
R
>
lambdaQuery
,
boolean
searchCount
)
{
R
entity
;
R
entity
=
lambdaQuery
.
getEntity
();
try
{
if
(
entity
==
null
)
{
entity
=
clazz
.
newInstance
();
try
{
}
catch
(
InstantiationException
|
IllegalAccessException
e
)
{
entity
=
clazz
.
newInstance
();
throw
new
BusinessException
(
"分页类型转换错误"
);
}
catch
(
InstantiationException
|
IllegalAccessException
e
)
{
throw
new
BusinessException
(
"分页类型转换错误"
);
}
}
}
// 已设置在lambdaQuery的字段实体类中不应该重复设置
// 已设置在lambdaQuery的字段实体类中不应该重复设置
String
[]
whereFields
=
getWhereFields
(
lambdaQuery
);
String
[]
whereFields
=
getWhereFields
(
lambdaQuery
,
clazz
);
BeanUtil
.
copyProperties
(
reqVo
,
entity
,
whereFields
);
BeanUtil
.
copyProperties
(
reqVo
,
entity
,
CopyOptions
.
create
().
setIgnoreProperties
(
whereFields
).
setIgnoreNullValue
(
true
).
setOverride
(
false
)
);
lambdaQuery
.
setEntity
(
entity
);
lambdaQuery
.
setEntity
(
entity
);
...
@@ -173,7 +179,7 @@ public abstract class PagingUtils {
...
@@ -173,7 +179,7 @@ public abstract class PagingUtils {
* @param <R>
* @param <R>
* @return
* @return
*/
*/
public
static
<
R
>
String
[]
getWhereFields
(
LambdaQueryWrapper
<
R
>
lambdaQuery
)
{
public
static
<
R
>
String
[]
getWhereFields
(
Wrapper
<
R
>
lambdaQuery
,
Class
<
R
>
clazz
)
{
List
<
String
>
fields
=
new
ArrayList
<>();
List
<
String
>
fields
=
new
ArrayList
<>();
MergeSegments
expression
=
lambdaQuery
.
getExpression
();
MergeSegments
expression
=
lambdaQuery
.
getExpression
();
...
@@ -187,13 +193,30 @@ public abstract class PagingUtils {
...
@@ -187,13 +193,30 @@ public abstract class PagingUtils {
if
(
nextSegment
instanceof
SqlKeyword
)
{
if
(
nextSegment
instanceof
SqlKeyword
)
{
ISqlSegment
sqlSegment
=
normal
.
get
(
i
);
ISqlSegment
sqlSegment
=
normal
.
get
(
i
);
// 数据库字段名
String
segment
=
sqlSegment
.
getSqlSegment
();
String
segment
=
sqlSegment
.
getSqlSegment
();
if
(
segment
.
startsWith
(
"#{"
))
{
if
(
segment
.
startsWith
(
"#{"
))
{
continue
;
continue
;
}
}
String
field
=
StrUtil
.
toCamelCase
(
segment
.
toLowerCase
());
String
fieldName
=
StrUtil
.
toCamelCase
(
segment
.
toLowerCase
());
fields
.
add
(
field
);
// 判断使用注解的需要获取类真实字段名
Field
[]
declaredFields
=
clazz
.
getDeclaredFields
();
for
(
Field
field
:
declaredFields
)
{
TableId
tableId
=
field
.
getAnnotation
(
TableId
.
class
);
if
(
tableId
!=
null
&&
segment
.
equals
(
tableId
.
value
()))
{
fieldName
=
field
.
getName
();
break
;
}
TableField
tableField
=
field
.
getAnnotation
(
TableField
.
class
);
if
(
tableField
!=
null
&&
segment
.
equals
(
tableField
.
value
()))
{
fieldName
=
field
.
getName
();
break
;
}
}
fields
.
add
(
fieldName
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment