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
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
panwangnan
schbrain-parent
Commits
96095cb5
Commit
96095cb5
authored
May 17, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update BizId to support any type
parent
714cefae
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
34 additions
and
37 deletions
+34
-37
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/annotation/BizId.java
...ain/framework/autoconfigure/mybatis/annotation/BizId.java
+3
-2
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
...ain/framework/autoconfigure/mybatis/base/BaseService.java
+6
-6
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
...framework/autoconfigure/mybatis/base/BaseServiceImpl.java
+7
-10
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdColumnField.java
...framework/autoconfigure/mybatis/biz/BizIdColumnField.java
+10
-8
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdHelper.java
...rain/framework/autoconfigure/mybatis/biz/BizIdHelper.java
+1
-2
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java
...ork/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java
+3
-5
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdType.java
...hbrain/framework/autoconfigure/mybatis/biz/BizIdType.java
+2
-2
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/DefaultTableConstraintChecker.java
...ure/mybatis/constraint/DefaultTableConstraintChecker.java
+2
-2
No files found.
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/annotation/BizId.java
View file @
96095cb5
package
com.schbrain.framework.autoconfigure.mybatis.annotation
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdType
;
import
java.lang.annotation.*
;
...
...
@@ -16,7 +17,7 @@ public @interface BizId {
/**
* 逻辑主键列名,为空时取字段名
*
* @see
com.schbrain.framework.autoconfigure.mybatis.core.
BizIdColumnField
* @see BizIdColumnField
*/
String
value
()
default
""
;
...
...
@@ -25,4 +26,4 @@ public @interface BizId {
*/
BizIdType
type
()
default
BizIdType
.
ID_WORKER
;
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java
View file @
96095cb5
...
...
@@ -29,31 +29,31 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取记录
*/
T
getByBizId
(
String
bizId
);
T
getByBizId
(
Object
bizId
);
/**
* 根据业务主键获取记录
*
* @param throwIfNotFound 未获取到记录时是否抛异常
*/
T
getByBizId
(
String
bizId
,
boolean
throwIfNotFound
);
T
getByBizId
(
Object
bizId
,
boolean
throwIfNotFound
);
/**
* 根据业务主键获取记录
*
* @param notFoundSupplier 未获取到记录时是否抛异常
*/
T
getByBizId
(
String
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
T
getByBizId
(
Object
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
);
/**
* 根据业务主键获取
*/
List
<
T
>
listByBizIds
(
Collection
<
String
>
bizIds
);
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
);
/**
* 根据业务主键获取
*/
Map
<
String
,
T
>
getMapByBizIds
(
Collection
<
String
>
bizIds
);
<
K
>
Map
<
K
,
T
>
getMapByBizIds
(
Collection
<
K
>
bizIds
);
/**
* 根据 id 更新,null 会被更新为 null
...
...
@@ -70,4 +70,4 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/
boolean
updateBatchByIdsWithNull
(
Collection
<
T
>
entityList
,
int
batchSize
);
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java
View file @
96095cb5
...
...
@@ -8,8 +8,8 @@ import com.schbrain.common.exception.BaseException;
import
com.schbrain.common.util.StreamUtils
;
import
com.schbrain.common.util.support.ValidateSupport
;
import
com.schbrain.framework.autoconfigure.mybatis.annotation.BizId
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper
;
import
com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField
;
import
com.schbrain.framework.autoconfigure.mybatis.exception.NoSuchRecordException
;
import
org.apache.ibatis.binding.MapperMethod.ParamMap
;
import
org.springframework.beans.factory.InitializingBean
;
...
...
@@ -60,12 +60,12 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
T
getByBizId
(
String
bizId
)
{
public
T
getByBizId
(
Object
bizId
)
{
return
getByBizId
(
bizId
,
false
);
}
@Override
public
T
getByBizId
(
String
bizId
,
boolean
throwIfNotFound
)
{
public
T
getByBizId
(
Object
bizId
,
boolean
throwIfNotFound
)
{
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
=
null
;
if
(
throwIfNotFound
)
{
notFoundSupplier
=
()
->
new
NoSuchRecordException
(
entityClass
);
...
...
@@ -74,7 +74,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
T
getByBizId
(
String
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
public
T
getByBizId
(
Object
bizId
,
Supplier
<?
extends
RuntimeException
>
notFoundSupplier
)
{
assertBidColumnFieldExist
();
T
entity
=
query
().
eq
(
bizIdColumnField
.
getColumnName
(),
bizId
).
one
();
if
(
entity
==
null
&&
notFoundSupplier
!=
null
)
{
...
...
@@ -84,7 +84,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
List
<
T
>
listByBizIds
(
Collection
<
String
>
bizIds
)
{
public
<
K
>
List
<
T
>
listByBizIds
(
Collection
<
K
>
bizIds
)
{
assertBidColumnFieldExist
();
if
(
isEmpty
(
bizIds
))
{
return
Collections
.
emptyList
();
...
...
@@ -93,7 +93,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public
Map
<
String
,
T
>
getMapByBizIds
(
Collection
<
String
>
bizIds
)
{
public
<
K
>
Map
<
K
,
T
>
getMapByBizIds
(
Collection
<
K
>
bizIds
)
{
assertBidColumnFieldExist
();
if
(
isEmpty
(
bizIds
))
{
return
Collections
.
emptyMap
();
...
...
@@ -129,9 +129,6 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
if
(
this
.
bizIdColumnField
!=
null
)
{
throw
new
BaseException
(
String
.
format
(
"@BizId can't more than one in Class: \"%s\""
,
entityClass
.
getName
()));
}
if
(
bizId
.
getType
()
!=
String
.
class
)
{
throw
new
BaseException
(
"@BizId only support String field"
);
}
this
.
bizIdColumnField
=
new
BizIdColumnField
(
entityClass
,
bizId
);
BizIdHelper
.
putBizColumnField
(
entityClass
,
bizIdColumnField
);
},
field
->
field
.
isAnnotationPresent
(
BizId
.
class
));
...
...
@@ -147,4 +144,4 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return
mapperClass
.
getName
()
+
StringPool
.
DOT
+
"alwaysUpdateSomeColumnById"
;
}
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/
core
/BizIdColumnField.java
→
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/
biz
/BizIdColumnField.java
View file @
96095cb5
package
com.schbrain.framework.autoconfigure.mybatis.
core
;
package
com.schbrain.framework.autoconfigure.mybatis.
biz
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.framework.autoconfigure.mybatis.annotation.BizId
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper
;
import
lombok.Data
;
import
java.lang.invoke.MethodHandle
;
...
...
@@ -29,22 +28,25 @@ public class BizIdColumnField {
this
.
annotation
=
bizIdField
.
getAnnotation
(
BizId
.
class
);
this
.
columnName
=
BizIdHelper
.
getColumnName
(
entityClass
,
bizIdField
,
this
.
annotation
);
try
{
this
.
bizIdFieldGetterMethodHandle
=
privateLookupIn
(
entityClass
,
lookup
()).
findGetter
(
entityClass
,
bizIdField
.
getName
(),
String
.
class
);
this
.
bizIdFieldSetterMethodHandle
=
privateLookupIn
(
entityClass
,
lookup
()).
findSetter
(
entityClass
,
bizIdField
.
getName
(),
String
.
class
);
Class
<?>
fieldType
=
bizIdField
.
getType
();
Lookup
lookup
=
privateLookupIn
(
entityClass
,
lookup
());
this
.
bizIdFieldGetterMethodHandle
=
lookup
.
findGetter
(
entityClass
,
bizIdField
.
getName
(),
fieldType
);
this
.
bizIdFieldSetterMethodHandle
=
lookup
.
findSetter
(
entityClass
,
bizIdField
.
getName
(),
fieldType
);
}
catch
(
NoSuchFieldException
|
IllegalAccessException
e
)
{
throw
new
BaseException
(
e
.
getMessage
(),
e
);
}
}
public
<
T
>
String
getValue
(
T
entity
)
{
@SuppressWarnings
(
"unchecked"
)
public
<
V
,
T
>
V
getValue
(
T
entity
)
{
try
{
return
(
String
)
bizIdFieldGetterMethodHandle
.
invoke
(
entity
);
return
(
V
)
bizIdFieldGetterMethodHandle
.
invoke
(
entity
);
}
catch
(
Throwable
e
)
{
throw
new
BaseException
(
e
.
getMessage
(),
e
);
}
}
public
<
T
>
void
setValue
(
T
entity
,
String
value
)
{
public
<
V
,
T
>
void
setValue
(
T
entity
,
V
value
)
{
try
{
bizIdFieldSetterMethodHandle
.
invoke
(
entity
,
value
);
}
catch
(
Throwable
e
)
{
...
...
@@ -52,4 +54,4 @@ public class BizIdColumnField {
}
}
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdHelper.java
View file @
96095cb5
...
...
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import
com.baomidou.mybatisplus.core.metadata.TableInfoHelper
;
import
com.baomidou.mybatisplus.core.toolkit.StringUtils
;
import
com.schbrain.framework.autoconfigure.mybatis.annotation.BizId
;
import
com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField
;
import
org.apache.ibatis.session.Configuration
;
import
java.lang.reflect.Field
;
...
...
@@ -45,4 +44,4 @@ public class BizIdHelper {
return
bizIdField
.
getName
();
}
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdInjectInterceptor.java
View file @
96095cb5
package
com.schbrain.framework.autoconfigure.mybatis.biz
;
import
com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor
;
import
com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.ibatis.executor.Executor
;
import
org.apache.ibatis.mapping.MappedStatement
;
import
org.apache.ibatis.mapping.SqlCommandType
;
...
...
@@ -32,11 +30,11 @@ public class BizIdInjectInterceptor implements InnerInterceptor {
return
;
}
if
(
bizIdType
==
BizIdType
.
ID_WORKER
)
{
String
bizIdValue
=
bizColumnField
.
getValue
(
entity
);
if
(
StringUtils
.
isBlank
(
bizIdValue
)
)
{
Object
bizIdValue
=
bizColumnField
.
getValue
(
entity
);
if
(
bizIdValue
==
null
)
{
bizColumnField
.
setValue
(
entity
,
bizIdType
.
generateBizId
(
entity
));
}
}
}
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/biz/BizIdType.java
View file @
96095cb5
...
...
@@ -28,8 +28,8 @@ public enum BizIdType {
/**
* 生成 bizId 的值
*/
public
String
generateBizId
(
Object
entity
)
{
public
Object
generateBizId
(
Object
entity
)
{
return
generator
.
generate
(
entity
);
}
}
\ No newline at end of file
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/DefaultTableConstraintChecker.java
View file @
96095cb5
package
com.schbrain.framework.autoconfigure.mybatis.constraint
;
import
com.mysql.cj.MysqlType
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField
;
import
com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper
;
import
com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField
;
import
com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException
;
import
static
com
.
schbrain
.
framework
.
autoconfigure
.
mybatis
.
constant
.
MybatisConstants
.*;
...
...
@@ -141,4 +141,4 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
table
.
addError
(
new
TableConstraintException
(
table
.
getTableName
(),
columnName
,
errorMsg
));
}
}
\ No newline at end of file
}
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