mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: @ColumnMask,typeHandler 一起使用时冲突的问题 close #I8EXFW
This commit is contained in:
parent
41aaeb69cb
commit
9bc74d7e4e
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package com.mybatisflex.core.mask;
|
||||||
|
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
|
import java.sql.CallableStatement;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class CompositeMaskTypeHandler implements TypeHandler<Object> {
|
||||||
|
|
||||||
|
private final String maskType;
|
||||||
|
private final TypeHandler<Object> typeHandler;
|
||||||
|
|
||||||
|
public CompositeMaskTypeHandler(String maskType, TypeHandler<Object> typeHandler) {
|
||||||
|
this.maskType = maskType;
|
||||||
|
this.typeHandler = typeHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
typeHandler.setParameter(ps, i, parameter, jdbcType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
return MaskManager.mask(maskType, typeHandler.getResult(rs, columnName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
return MaskManager.mask(maskType, typeHandler.getResult(rs, columnIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
return MaskManager.mask(maskType, typeHandler.getResult(cs, columnIndex));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,6 +16,7 @@
|
|||||||
package com.mybatisflex.core.table;
|
package com.mybatisflex.core.table;
|
||||||
|
|
||||||
import com.mybatisflex.core.FlexGlobalConfig;
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
|
import com.mybatisflex.core.mask.CompositeMaskTypeHandler;
|
||||||
import com.mybatisflex.core.mask.MaskTypeHandler;
|
import com.mybatisflex.core.mask.MaskTypeHandler;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.apache.ibatis.session.Configuration;
|
import org.apache.ibatis.session.Configuration;
|
||||||
@ -54,6 +55,11 @@ public class ColumnInfo {
|
|||||||
*/
|
*/
|
||||||
protected TypeHandler typeHandler;
|
protected TypeHandler typeHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最终使用和构建出来的 typeHandler
|
||||||
|
*/
|
||||||
|
protected TypeHandler buildTypeHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据脱敏类型。
|
* 数据脱敏类型。
|
||||||
*/
|
*/
|
||||||
@ -107,23 +113,33 @@ public class ColumnInfo {
|
|||||||
|
|
||||||
public TypeHandler buildTypeHandler(Configuration configuration) {
|
public TypeHandler buildTypeHandler(Configuration configuration) {
|
||||||
|
|
||||||
//优先使用自定义的 typeHandler
|
if (buildTypeHandler != null) {
|
||||||
if (typeHandler != null) {
|
return buildTypeHandler;
|
||||||
return typeHandler;
|
|
||||||
}
|
|
||||||
//枚举
|
|
||||||
else if (propertyType.isEnum()) {
|
|
||||||
if (configuration == null){
|
|
||||||
configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
|
|
||||||
}
|
|
||||||
this.typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
|
|
||||||
}
|
|
||||||
//若用户未定义 typeHandler,而配置了数据脱敏,则使用脱敏的 handler 处理
|
|
||||||
else if (StringUtil.isNotBlank(maskType)) {
|
|
||||||
typeHandler = new MaskTypeHandler(maskType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeHandler;
|
//脱敏规则配置
|
||||||
|
else if (StringUtil.isNotBlank(maskType)) {
|
||||||
|
if (typeHandler != null) {
|
||||||
|
buildTypeHandler = new CompositeMaskTypeHandler(maskType, typeHandler);
|
||||||
|
} else {
|
||||||
|
buildTypeHandler = new MaskTypeHandler(maskType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//用户自定义的 typeHandler
|
||||||
|
else if (typeHandler != null) {
|
||||||
|
buildTypeHandler = typeHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
//枚举
|
||||||
|
else if (propertyType.isEnum()) {
|
||||||
|
if (configuration == null) {
|
||||||
|
configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration();
|
||||||
|
}
|
||||||
|
buildTypeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildTypeHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTypeHandler(TypeHandler typeHandler) {
|
public void setTypeHandler(TypeHandler typeHandler) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user