Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
489d7810
Commit
489d7810
authored
Aug 24, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
9354b293
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
67 additions
and
37 deletions
+67
-37
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
...til/src/main/java/com/schbrain/common/util/TreeUtils.java
+41
-16
commons/web-common/src/main/java/com/schbrain/common/web/ExceptionHandingConfiguration.java
...om/schbrain/common/web/ExceptionHandingConfiguration.java
+1
-1
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultExceptionTranslator.java
...rain/common/web/exception/DefaultExceptionTranslator.java
+2
-2
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseEntityWithLogicDelete.java
...autoconfigure/mybatis/base/BaseEntityWithLogicDelete.java
+2
-2
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
...ain/framework/autoconfigure/mybatis/base/BaseService.java
+3
-3
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
...framework/autoconfigure/mybatis/base/BaseServiceImpl.java
+18
-13
No files found.
commons/common-util/src/main/java/com/schbrain/common/util/TreeUtils.java
View file @
489d7810
package
com.schbrain.common.util
;
package
com.schbrain.common.util
;
import
cn.hutool.core.collection.ListUtil
;
import
cn.hutool.core.collection.ListUtil
;
import
com.google.common.collect.Maps
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.MapUtils
;
import
javax.annotation.Nullable
;
import
javax.annotation.Nullable
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.Comparator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.function.BiConsumer
;
import
java.util.function.BiConsumer
;
import
java.util.function.Function
;
import
java.util.function.Function
;
...
@@ -55,23 +54,49 @@ public class TreeUtils {
...
@@ -55,23 +54,49 @@ public class TreeUtils {
return
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
parentId
);
return
doBuildTree
(
keyExtractor
,
childrenSetter
,
childMapper
,
parentWithSubNodes
,
childrenComparator
,
parentId
);
}
}
public
static
<
T
,
E
>
List
<
E
>
buildNodeList
(
List
<
T
>
treeList
,
public
static
<
T
,
E
>
List
<
E
>
getParents
(
E
id
,
List
<
T
>
nodeList
,
Function
<
T
,
E
>
keyMapper
,
Function
<
T
,
E
>
parentMapper
,
boolean
includeSelf
)
{
Function
<
T
,
List
<
T
>>
childrenGetter
,
// toMap 不允许 value 为空,当 parentId 为空时,单独处理下
Function
<
T
,
E
>
mapper
)
{
Map
<
E
,
E
>
parentMap
=
Maps
.
newHashMapWithExpectedSize
(
nodeList
.
size
());
for
(
T
node
:
nodeList
)
{
parentMap
.
put
(
keyMapper
.
apply
(
node
),
parentMapper
.
apply
(
node
));
}
return
getParents
(
id
,
parentMap
,
includeSelf
);
}
public
static
<
T
>
List
<
T
>
getParents
(
T
id
,
Map
<
T
,
T
>
parentMap
,
boolean
includeSelf
)
{
List
<
T
>
parentIds
=
new
LinkedList
<>();
if
(
includeSelf
)
{
parentIds
.
add
(
id
);
}
if
(
MapUtils
.
isEmpty
(
parentMap
))
{
return
parentIds
;
}
return
getParents
(
id
,
parentMap
,
parentIds
);
}
public
static
<
T
,
E
>
List
<
E
>
buildNodeList
(
List
<
T
>
tree
,
Function
<
T
,
List
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
)
{
List
<
E
>
nodes
=
new
ArrayList
<>();
List
<
E
>
nodes
=
new
ArrayList
<>();
doBuildNodeList
(
treeList
,
childrenGetter
,
mapper
,
nodes
);
if
(
CollectionUtils
.
isEmpty
(
tree
))
{
return
nodes
;
}
doBuildNodeList
(
tree
,
childGetter
,
mapper
,
nodes
);
return
nodes
;
return
nodes
;
}
}
private
static
<
E
,
T
>
void
doBuildNodeList
(
List
<
T
>
treeList
,
private
static
<
E
,
T
>
void
doBuildNodeList
(
List
<
T
>
tree
,
Function
<
T
,
List
<
T
>>
childGetter
,
Function
<
T
,
E
>
mapper
,
List
<
E
>
nodes
)
{
Function
<
T
,
List
<
T
>>
childrenGetter
,
tree
.
forEach
(
node
->
{
Function
<
T
,
E
>
mapper
,
nodes
.
add
(
mapper
.
apply
(
node
));
List
<
E
>
nodes
)
{
doBuildNodeList
(
childGetter
.
apply
(
node
),
childGetter
,
mapper
,
nodes
);
if
(
CollectionUtils
.
isEmpty
(
treeList
))
{
});
return
;
}
private
static
<
T
>
List
<
T
>
getParents
(
T
id
,
Map
<
T
,
T
>
parentMap
,
List
<
T
>
parentIds
)
{
T
parentId
=
parentMap
.
get
(
id
);
if
(
parentId
==
null
)
{
return
parentIds
;
}
}
nodes
.
addAll
(
StreamUtils
.
toList
(
treeList
,
mapper
)
);
parentIds
.
add
(
0
,
parentId
);
treeList
.
forEach
(
tree
->
doBuildNodeList
(
childrenGetter
.
apply
(
tree
),
childrenGetter
,
mapper
,
nodes
)
);
return
getParents
(
parentId
,
parentMap
,
parentIds
);
}
}
private
static
<
E
,
K
,
T
>
List
<
E
>
doBuildTree
(
Function
<
T
,
K
>
keyExtractor
,
private
static
<
E
,
K
,
T
>
List
<
E
>
doBuildTree
(
Function
<
T
,
K
>
keyExtractor
,
...
...
commons/web-common/src/main/java/com/schbrain/common/web/ExceptionHandingConfiguration.java
View file @
489d7810
...
@@ -24,7 +24,7 @@ public class ExceptionHandingConfiguration {
...
@@ -24,7 +24,7 @@ public class ExceptionHandingConfiguration {
@Bean
@Bean
@ConditionalOnMissingBean
@ConditionalOnMissingBean
public
ExceptionTranslator
<
ResponseDTO
<
String
>>
defaultExceptionTranslator
()
{
public
ExceptionTranslator
<
ResponseDTO
<
Void
>>
defaultExceptionTranslator
()
{
return
new
DefaultExceptionTranslator
();
return
new
DefaultExceptionTranslator
();
}
}
...
...
commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultExceptionTranslator.java
View file @
489d7810
...
@@ -10,12 +10,12 @@ import org.springframework.core.Ordered;
...
@@ -10,12 +10,12 @@ import org.springframework.core.Ordered;
* @author liaozan
* @author liaozan
* @since 2023-06-01
* @since 2023-06-01
*/
*/
public
class
DefaultExceptionTranslator
implements
ExceptionTranslator
<
ResponseDTO
<
String
>>
{
public
class
DefaultExceptionTranslator
implements
ExceptionTranslator
<
ResponseDTO
<
Void
>>
{
private
final
boolean
isProduction
=
EnvUtils
.
isProduction
();
private
final
boolean
isProduction
=
EnvUtils
.
isProduction
();
@Override
@Override
public
ResponseDTO
<
String
>
translate
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
)
{
public
ResponseDTO
<
Void
>
translate
(
Throwable
throwable
,
int
code
,
int
action
,
String
message
)
{
if
(
throwable
instanceof
BaseException
)
{
if
(
throwable
instanceof
BaseException
)
{
return
ResponseDTO
.
error
((
BaseException
)
throwable
);
return
ResponseDTO
.
error
((
BaseException
)
throwable
);
}
}
...
...
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseEntityWithLogicDelete.java
View file @
489d7810
...
@@ -20,7 +20,7 @@ public class BaseEntityWithLogicDelete extends BaseEntity {
...
@@ -20,7 +20,7 @@ public class BaseEntityWithLogicDelete extends BaseEntity {
*/
*/
@TableLogic
@TableLogic
@TableField
(
value
=
MybatisConstants
.
DELETED
,
select
=
false
)
@TableField
(
value
=
MybatisConstants
.
DELETED
,
select
=
false
)
protected
b
oolean
deleted
;
protected
B
oolean
deleted
;
/**
/**
* 逻辑删除版本
* 逻辑删除版本
...
@@ -29,4 +29,4 @@ public class BaseEntityWithLogicDelete extends BaseEntity {
...
@@ -29,4 +29,4 @@ public class BaseEntityWithLogicDelete extends BaseEntity {
@TableField
(
value
=
MybatisConstants
.
DELETE_VERSION
,
select
=
false
)
@TableField
(
value
=
MybatisConstants
.
DELETE_VERSION
,
select
=
false
)
protected
Long
deleteVersion
;
protected
Long
deleteVersion
;
}
}
\ No newline at end of file
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
View file @
489d7810
package
com.schbrain.framework.autoconfigure.mybatis.base
;
package
com.schbrain.framework.autoconfigure.mybatis.base
;
import
com.baomidou.mybatisplus.core.toolkit.support.SFunction
;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
java.util.Collection
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
public
interface
BaseService
<
T
extends
BaseEntity
>
extends
IService
<
T
>
{
public
interface
BaseService
<
T
extends
BaseEntity
>
extends
IService
<
T
>
{
...
@@ -32,7 +32,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
...
@@ -32,7 +32,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
/**
* 根据 id 获取
* 根据 id 获取
*/
*/
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
Function
<
T
,
V
>
mapper
);
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
S
Function
<
T
,
V
>
mapper
);
/**
/**
* 根据业务主键获取记录
* 根据业务主键获取记录
...
@@ -66,7 +66,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
...
@@ -66,7 +66,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
/**
* 根据业务主键获取
* 根据业务主键获取
*/
*/
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
Function
<
T
,
V
>
mapper
);
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
S
Function
<
T
,
V
>
mapper
);
/**
/**
* 根据 id 更新,null 会被更新为 null
* 根据 id 更新,null 会被更新为 null
...
...
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
View file @
489d7810
...
@@ -2,6 +2,7 @@ package com.schbrain.framework.autoconfigure.mybatis.base;
...
@@ -2,6 +2,7 @@ package com.schbrain.framework.autoconfigure.mybatis.base;
import
com.baomidou.mybatisplus.core.toolkit.Constants
;
import
com.baomidou.mybatisplus.core.toolkit.Constants
;
import
com.baomidou.mybatisplus.core.toolkit.StringPool
;
import
com.baomidou.mybatisplus.core.toolkit.StringPool
;
import
com.baomidou.mybatisplus.core.toolkit.support.SFunction
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.baomidou.mybatisplus.extension.toolkit.SqlHelper
;
import
com.baomidou.mybatisplus.extension.toolkit.SqlHelper
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.common.exception.BaseException
;
...
@@ -22,7 +23,6 @@ import java.util.Collection;
...
@@ -22,7 +23,6 @@ import java.util.Collection;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
import
java.util.function.Supplier
;
/**
/**
...
@@ -34,6 +34,10 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
...
@@ -34,6 +34,10 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Nullable
@Nullable
private
BizIdColumnField
bizIdColumnField
;
private
BizIdColumnField
bizIdColumnField
;
private
static
<
T
>
SFunction
<
T
,
T
>
identity
()
{
return
any
->
any
;
}
@Override
@Override
public
T
getById
(
Serializable
id
)
{
public
T
getById
(
Serializable
id
)
{
return
getById
((
Long
)
id
,
false
);
return
getById
((
Long
)
id
,
false
);
...
@@ -59,15 +63,17 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
...
@@ -59,15 +63,17 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
@Override
public
Map
<
Long
,
T
>
getMapByIds
(
Collection
<
Long
>
ids
)
{
public
Map
<
Long
,
T
>
getMapByIds
(
Collection
<
Long
>
ids
)
{
return
getMapByIds
(
ids
,
Function
.
identity
());
return
getMapByIds
(
ids
,
identity
());
}
}
@Override
@Override
public
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
Function
<
T
,
V
>
mapper
)
{
public
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
S
Function
<
T
,
V
>
mapper
)
{
if
(
isEmpty
(
ids
))
{
if
(
isEmpty
(
ids
))
{
return
Collections
.
emptyMap
();
return
Collections
.
emptyMap
();
}
}
return
StreamUtils
.
toMap
(
listByIds
(
ids
),
T:
:
getId
,
mapper
);
// noinspection unchecked
List
<
T
>
dataList
=
lambdaQuery
().
select
(
T:
:
getId
,
mapper
).
in
(
T:
:
getId
,
ids
).
list
();
return
StreamUtils
.
toMap
(
dataList
,
T:
:
getId
,
mapper
);
}
}
@Override
@Override
...
@@ -86,8 +92,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
...
@@ -86,8 +92,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
@Override
public
T
getByBizId
(
Object
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
public
T
getByBizId
(
Object
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
assertBidColumnFieldExist
();
T
entity
=
query
().
eq
(
getBidColumnField
().
getColumnName
(),
bizId
).
one
();
T
entity
=
query
().
eq
(
bizIdColumnField
.
getColumnName
(),
bizId
).
one
();
if
(
entity
==
null
&&
notFoundSupplier
!=
null
)
{
if
(
entity
==
null
&&
notFoundSupplier
!=
null
)
{
throw
notFoundSupplier
.
get
();
throw
notFoundSupplier
.
get
();
}
}
...
@@ -96,25 +101,24 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
...
@@ -96,25 +101,24 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override
@Override
public
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
)
{
public
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
)
{
assertBidColumnFieldExist
();
if
(
isEmpty
(
bizIds
))
{
if
(
isEmpty
(
bizIds
))
{
return
Collections
.
emptyList
();
return
Collections
.
emptyList
();
}
}
return
query
().
in
(
bizIdColumnField
.
getColumnName
(),
bizIds
).
list
();
return
query
().
in
(
getBidColumnField
()
.
getColumnName
(),
bizIds
).
list
();
}
}
@Override
@Override
public
<
K
>
Map
<
K
,
T
>
getMapByBizIds
(
Collection
<
K
>
bizIds
)
{
public
<
K
>
Map
<
K
,
T
>
getMapByBizIds
(
Collection
<
K
>
bizIds
)
{
return
getMapByBizIds
(
bizIds
,
Function
.
identity
());
return
getMapByBizIds
(
bizIds
,
identity
());
}
}
@Override
@Override
public
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
Function
<
T
,
V
>
mapper
)
{
public
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
mapper
)
{
assertBidColumnFieldExist
();
if
(
isEmpty
(
bizIds
))
{
if
(
isEmpty
(
bizIds
))
{
return
Collections
.
emptyMap
();
return
Collections
.
emptyMap
();
}
}
return
StreamUtils
.
toMap
(
listByBizIds
(
bizIds
),
entity
->
bizIdColumnField
.
getValue
(
entity
),
mapper
);
// How to get the mapper fieldName ?
return
StreamUtils
.
toMap
(
listByBizIds
(
bizIds
),
entity
->
getBidColumnField
().
getValue
(
entity
),
mapper
);
}
}
@Override
@Override
...
@@ -150,10 +154,11 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
...
@@ -150,10 +154,11 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
},
field
->
field
.
isAnnotationPresent
(
BizId
.
class
));
},
field
->
field
.
isAnnotationPresent
(
BizId
.
class
));
}
}
pr
ivate
void
assertBidColumnFieldExist
()
{
pr
otected
BizIdColumnField
getBidColumnField
()
{
if
(
bizIdColumnField
==
null
)
{
if
(
bizIdColumnField
==
null
)
{
throw
new
BaseException
(
String
.
format
(
"@BizId not exist in Class: \"%s\""
,
entityClass
.
getName
()));
throw
new
BaseException
(
String
.
format
(
"@BizId not exist in Class: \"%s\""
,
entityClass
.
getName
()));
}
}
return
bizIdColumnField
;
}
}
private
String
getUpdateByIdWithNullStatementId
(
Class
<
M
>
mapperClass
)
{
private
String
getUpdateByIdWithNullStatementId
(
Class
<
M
>
mapperClass
)
{
...
...
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