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
64312418
Commit
64312418
authored
Oct 22, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more shortcut method for BaseService
parent
37c03985
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
13 deletions
+132
-13
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
...ain/framework/autoconfigure/mybatis/base/BaseService.java
+53
-5
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
...framework/autoconfigure/mybatis/base/BaseServiceImpl.java
+79
-8
No files found.
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
View file @
64312418
...
...
@@ -22,6 +22,25 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/
T
getById
(
Long
id
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
/**
* 根据 id 获取记录
*/
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
);
/**
* 根据 id 获取记录
*
* @param throwIfNotFound 未获取到记录时是否抛异常
*/
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
,
boolean
throwIfNotFound
);
/**
* 根据 id 获取记录
*
* @param notFoundSupplier 未获取到记录时的异常处理
*/
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
/**
* 根据 id 获取
*/
...
...
@@ -30,7 +49,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据 id 获取
*/
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
mapper
);
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
column
);
/**
* 根据业务主键获取记录
...
...
@@ -40,22 +59,51 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取记录
*
* @param throwIfNotFound 未获取到记录时是否抛异常
* @param throw
s
IfNotFound 未获取到记录时是否抛异常
*/
T
getByBizId
(
Object
bizId
,
boolean
throwIfNotFound
);
T
getByBizId
(
Object
bizId
,
boolean
throw
s
IfNotFound
);
/**
* 根据业务主键获取记录
*
* @param notFoundSupplier 未获取到记录时
是否抛异常
* @param notFoundSupplier 未获取到记录时
的异常处理
*/
T
getByBizId
(
Object
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
/**
* 根据业务主键获取记录
*/
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
);
/**
* 根据业务主键获取记录
*
* @param throwsIfNotFound 未获取到记录时是否抛异常
*/
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
,
boolean
throwsIfNotFound
);
/**
* 根据业务主键获取记录
*
* @param notFoundSupplier 未获取到记录时的异常处理
*/
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
/**
* 根据业务主键获取
*/
<
V
>
List
<
V
>
listByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
column
);
/**
* 根据业务主键获取
*/
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
);
/**
* 根据业务主键获取
*/
<
K
,
V
>
List
<
V
>
listByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
column
);
/**
* 根据业务主键获取
*/
...
...
@@ -64,7 +112,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取
*/
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
mapper
);
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
column
);
/**
* 根据 id 更新,null 会被更新为 null
...
...
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
View file @
64312418
...
...
@@ -58,9 +58,32 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return
entity
;
}
@Override
public
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
)
{
return
getById
(
id
,
column
,
false
);
}
@Override
public
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
,
boolean
throwIfNotFound
)
{
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
=
null
;
if
(
throwIfNotFound
)
{
notFoundSupplier
=
()
->
new
NoSuchRecordException
(
entityClass
);
}
return
getById
(
id
,
column
,
notFoundSupplier
);
}
@Override
public
<
V
>
V
getById
(
Long
id
,
SFunction
<
T
,
V
>
column
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
T
entity
=
lambdaQuery
().
select
(
column
).
eq
(
T:
:
getId
,
id
).
one
();
if
(
entity
==
null
&&
notFoundSupplier
!=
null
)
{
throw
notFoundSupplier
.
get
();
}
return
entity
==
null
?
null
:
column
.
apply
(
entity
);
}
@Override
public
Map
<
Long
,
T
>
getMapByIds
(
Collection
<
Long
>
ids
)
{
// Cannot call the override method here, because override method use
mapper
to judge the fields to select
// Cannot call the override method here, because override method use
column
to judge the fields to select
if
(
isEmpty
(
ids
))
{
return
emptyMap
();
}
...
...
@@ -68,13 +91,13 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
mapper
)
{
public
<
V
>
Map
<
Long
,
V
>
getMapByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
column
)
{
if
(
isEmpty
(
ids
))
{
return
emptyMap
();
}
// noinspection unchecked
List
<
T
>
dataList
=
lambdaQuery
().
select
(
T:
:
getId
,
mapper
).
in
(
T:
:
getId
,
ids
).
list
();
return
StreamUtils
.
toMap
(
dataList
,
T:
:
getId
,
mapper
);
List
<
T
>
dataList
=
lambdaQuery
().
select
(
T:
:
getId
,
column
).
in
(
T:
:
getId
,
ids
).
list
();
return
StreamUtils
.
toMap
(
dataList
,
T:
:
getId
,
column
);
}
@Override
...
...
@@ -100,17 +123,65 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return
entity
;
}
@Override
public
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
)
{
return
getByBizId
(
bizId
,
column
,
false
);
}
@Override
public
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
,
boolean
throwIfNotFound
)
{
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
=
null
;
if
(
throwIfNotFound
)
{
notFoundSupplier
=
()
->
new
NoSuchRecordException
(
entityClass
);
}
return
getByBizId
(
bizId
,
column
,
notFoundSupplier
);
}
@Override
public
<
V
>
V
getByBizId
(
Object
bizId
,
SFunction
<
T
,
V
>
column
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
T
entity
=
query
()
.
select
(
LambdaUtils
.
getColumnName
(
column
))
.
eq
(
getBidColumnField
().
getColumnName
(),
bizId
)
.
one
();
if
(
entity
==
null
&&
notFoundSupplier
!=
null
)
{
throw
notFoundSupplier
.
get
();
}
return
entity
==
null
?
null
:
column
.
apply
(
entity
);
}
@Override
public
<
V
>
List
<
V
>
listByIds
(
Collection
<
Long
>
ids
,
SFunction
<
T
,
V
>
column
)
{
if
(
isEmpty
(
ids
))
{
return
emptyList
();
}
List
<
T
>
dataList
=
lambdaQuery
().
select
(
column
).
in
(
T:
:
getId
,
ids
).
list
();
return
StreamUtils
.
toList
(
dataList
,
column
);
}
@Override
public
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
)
{
// Cannot call the override method here, because override method use column to judge the fields to select
if
(
isEmpty
(
bizIds
))
{
return
emptyList
();
}
return
query
().
in
(
getBidColumnField
().
getColumnName
(),
bizIds
).
list
();
}
@Override
public
<
K
,
V
>
List
<
V
>
listByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
column
)
{
if
(
isEmpty
(
bizIds
))
{
return
emptyList
();
}
List
<
T
>
dataList
=
query
()
.
select
(
LambdaUtils
.
getColumnName
(
column
))
.
in
(
getBidColumnField
().
getColumnName
(),
bizIds
)
.
list
();
return
StreamUtils
.
toList
(
dataList
,
column
);
}
@Override
public
<
K
>
Map
<
K
,
T
>
getMapByBizIds
(
Collection
<
K
>
bizIds
)
{
// Cannot call the override method here, because override method use
mapper
to judge the fields to select
// Cannot call the override method here, because override method use
column
to judge the fields to select
if
(
isEmpty
(
bizIds
))
{
return
emptyMap
();
}
...
...
@@ -118,16 +189,16 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
mapper
)
{
public
<
K
,
V
>
Map
<
K
,
V
>
getMapByBizIds
(
Collection
<
K
>
bizIds
,
SFunction
<
T
,
V
>
column
)
{
if
(
isEmpty
(
bizIds
))
{
return
emptyMap
();
}
String
bizIdColumnName
=
getBidColumnField
().
getColumnName
();
List
<
T
>
dataList
=
query
()
.
select
(
bizIdColumnName
,
LambdaUtils
.
getColumnName
(
mapper
))
.
select
(
bizIdColumnName
,
LambdaUtils
.
getColumnName
(
column
))
.
in
(
bizIdColumnName
,
bizIds
)
.
list
();
return
StreamUtils
.
toMap
(
dataList
,
entity
->
getBidColumnField
().
getValue
(
entity
),
mapper
);
return
StreamUtils
.
toMap
(
dataList
,
entity
->
getBidColumnField
().
getValue
(
entity
),
column
);
}
@Override
...
...
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