Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibizlab-boot-starters
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibizlab-boot-starters
提交
8ed0a3ff
提交
8ed0a3ff
编写于
9月 30, 2022
作者:
chenxiang@lab.ibiz5.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
NoSQL:MongoDB适配、更新空值处理
上级
8227c7fe
变更
4
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
97 行增加
和
21 行删除
+97
-21
EntityMongo.java
...ata/src/main/java/cn/ibizlab/util/domain/EntityMongo.java
+9
-0
CachedBeanCopier.java
...rc/main/java/cn/ibizlab/util/helper/CachedBeanCopier.java
+68
-0
IBZMongoSimpleRepository.java
.../cn/ibizlab/util/repository/IBZMongoSimpleRepository.java
+20
-3
CachedBeanCopier.java
...rc/main/java/cn/ibizlab/util/helper/CachedBeanCopier.java
+0
-18
未找到文件。
ibizlab-boot-starter-data/src/main/java/cn/ibizlab/util/domain/EntityMongo.java
浏览文件 @
8ed0a3ff
...
...
@@ -2,5 +2,14 @@ package cn.ibizlab.util.domain;
public
class
EntityMongo
extends
EntityBase
{
public
void
modify
(
String
field
,
Object
val
)
{
if
(
val
==
null
)
{
this
.
getFocusNull
().
add
(
field
.
toLowerCase
());
}
else
{
this
.
getFocusNull
().
remove
(
field
.
toLowerCase
());
}
}
}
ibizlab-boot-starter-data/src/main/java/cn/ibizlab/util/helper/CachedBeanCopier.java
0 → 100644
浏览文件 @
8ed0a3ff
package
cn
.
ibizlab
.
util
.
helper
;
import
cn.ibizlab.util.domain.EntityBase
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.FatalBeanException
;
import
java.beans.PropertyDescriptor
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
public
class
CachedBeanCopier
{
public
static
void
copy
(
Object
srcObj
,
Object
destObj
)
{
copy
(
srcObj
,
destObj
,
false
);
}
public
static
void
copy
(
Object
srcObj
,
Object
destObj
,
boolean
useConverter
)
{
if
(
srcObj
==
null
||
destObj
==
null
)
{
return
;
}
BeanUtils
.
copyProperties
(
srcObj
,
destObj
);
}
public
static
void
copyWithFocusNull
(
EntityBase
source
,
EntityBase
target
)
{
if
(
source
==
null
||
target
==
null
)
{
return
;
}
Class
<?>
actualEditable
=
target
.
getClass
();
Class
<?>
sourceClass
=
source
.
getClass
();
PropertyDescriptor
[]
targetPds
=
BeanUtils
.
getPropertyDescriptors
(
actualEditable
);
for
(
PropertyDescriptor
targetPd
:
targetPds
)
{
if
(
targetPd
.
getWriteMethod
()
==
null
)
{
continue
;
}
PropertyDescriptor
sourcePd
=
BeanUtils
.
getPropertyDescriptor
(
sourceClass
,
targetPd
.
getName
());
if
(
sourcePd
==
null
||
sourcePd
.
getReadMethod
()
==
null
)
{
continue
;
}
try
{
Method
readMethod
=
sourcePd
.
getReadMethod
();
if
(!
Modifier
.
isPublic
(
readMethod
.
getDeclaringClass
().
getModifiers
()))
{
readMethod
.
setAccessible
(
true
);
}
Object
value
=
readMethod
.
invoke
(
source
);
if
(
value
==
null
)
{
if
(
source
.
getFocusNull
().
contains
(
sourcePd
.
getName
().
toLowerCase
()))
{
setValue
(
target
,
targetPd
,
value
);
}
}
else
{
setValue
(
target
,
targetPd
,
value
);
}
}
catch
(
Exception
ex
)
{
throw
new
FatalBeanException
(
"Could not copy properties from source to target"
,
ex
);
}
}
}
private
static
void
setValue
(
Object
target
,
PropertyDescriptor
targetPd
,
Object
value
)
throws
IllegalAccessException
,
InvocationTargetException
{
Method
writeMethod
=
targetPd
.
getWriteMethod
();
if
(!
Modifier
.
isPublic
(
writeMethod
.
getDeclaringClass
().
getModifiers
()))
{
writeMethod
.
setAccessible
(
true
);
}
writeMethod
.
invoke
(
target
,
value
);
}
}
ibizlab-boot-starter-data/src/main/java/cn/ibizlab/util/repository/IBZMongoSimpleRepository.java
浏览文件 @
8ed0a3ff
package
cn
.
ibizlab
.
util
.
repository
;
import
cn.ibizlab.util.domain.EntityMongo
;
import
cn.ibizlab.util.filter.MongoQueryContext
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.mongodb.core.MongoOperations
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.repository.query.MongoEntityInformation
;
...
...
@@ -10,8 +11,9 @@ import org.springframework.data.mongodb.repository.support.SimpleMongoRepository
import
org.springframework.data.support.PageableExecutionUtils
;
import
java.util.List
;
import
java.util.Optional
;
public
class
IBZMongoSimpleRepository
<
T
,
ID
>
extends
SimpleMongoRepository
<
T
,
ID
>
implements
IBZMongoRepository
<
T
,
ID
>
{
public
class
IBZMongoSimpleRepository
<
T
extends
EntityMongo
,
ID
>
extends
SimpleMongoRepository
<
T
,
ID
>
implements
IBZMongoRepository
<
T
,
ID
>
{
private
final
MongoOperations
mongoOperations
;
private
final
MongoEntityInformation
<
T
,
ID
>
entityInformation
;
...
...
@@ -22,10 +24,25 @@ public class IBZMongoSimpleRepository<T, ID> extends SimpleMongoRepository<T, ID
this
.
mongoOperations
=
mongoOperations
;
}
@Override
public
<
S
extends
T
>
S
save
(
S
entity
)
{
//更新空值处理
ID
id
=
entityInformation
.
getId
(
entity
);
if
(
id
!=
null
)
{
Optional
<
T
>
op
=
findById
(
id
);
if
(
op
.
isPresent
())
{
T
t
=
op
.
get
();
CachedBeanCopier
.
copyWithFocusNull
(
entity
,
t
);
entity
=
(
S
)
t
;
}
}
return
super
.
save
(
entity
);
}
@Override
public
<
S
extends
T
>
Page
<
T
>
query
(
MongoQueryContext
mongoQueryContext
)
{
Query
query
=
mongoQueryContext
.
getSearchCond
();
query
.
with
(
mongoQueryContext
.
getPageable
());
query
.
with
(
mongoQueryContext
.
getPageable
());
if
(
mongoQueryContext
.
getPageSort
()!=
null
)
query
.
with
(
mongoQueryContext
.
getPageSort
());
List
<
T
>
list
=
mongoOperations
.
find
(
query
,
entityInformation
.
getJavaType
(),
entityInformation
.
getCollectionName
());
...
...
ibizlab-boot-starter/src/main/java/cn/ibizlab/util/helper/CachedBeanCopier.java
已删除
100644 → 0
浏览文件 @
8227c7fe
package
cn
.
ibizlab
.
util
.
helper
;
import
org.springframework.beans.BeanUtils
;
public
class
CachedBeanCopier
{
public
static
void
copy
(
Object
srcObj
,
Object
destObj
)
{
copy
(
srcObj
,
destObj
,
false
);
}
public
static
void
copy
(
Object
srcObj
,
Object
destObj
,
boolean
useConverter
)
{
if
(
srcObj
==
null
||
destObj
==
null
)
{
return
;
}
BeanUtils
.
copyProperties
(
srcObj
,
destObj
);
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录