mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
fix(data/mp): 修复普通枚举类型处理错误
This commit is contained in:
@@ -36,7 +36,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import top.continew.starter.core.constant.PropertiesConstants;
|
||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||
import top.continew.starter.data.mp.autoconfigure.idgenerator.MyBatisPlusIdGeneratorConfiguration;
|
||||
import top.continew.starter.data.mp.handler.MybatisBaseEnumTypeHandler;
|
||||
import top.continew.starter.data.mp.handler.CompositeBaseEnumTypeHandler;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -63,7 +63,8 @@ public class MybatisPlusAutoConfiguration {
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
|
||||
return properties -> properties.getConfiguration().setDefaultEnumTypeHandler(MybatisBaseEnumTypeHandler.class);
|
||||
return properties -> properties.getConfiguration()
|
||||
.setDefaultEnumTypeHandler(CompositeBaseEnumTypeHandler.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <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 top.continew.starter.data.mp.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import org.apache.ibatis.type.EnumTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.TypeException;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 复合枚举类型处理器(扩展 BaseEnum 支持)
|
||||
*
|
||||
* @author miemie(<a href="https://gitee.com/baomidou/mybatis-plus">MyBatis Plus</a>)
|
||||
* @author Charles7c
|
||||
* @see com.baomidou.mybatisplus.core.handlers.CompositeEnumTypeHandler
|
||||
* @since 2.7.3
|
||||
*/
|
||||
public class CompositeBaseEnumTypeHandler<E extends Enum<E>> implements TypeHandler<E> {
|
||||
|
||||
private static final Map<Class<?>, Boolean> MP_ENUM_CACHE = new ConcurrentHashMap<>();
|
||||
private static Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;
|
||||
private final TypeHandler<E> delegate;
|
||||
|
||||
public CompositeBaseEnumTypeHandler(Class<E> enumClassType) {
|
||||
if (enumClassType == null) {
|
||||
throw new IllegalArgumentException("Type argument cannot be null");
|
||||
}
|
||||
if (Boolean.TRUE.equals(CollectionUtils
|
||||
.computeIfAbsent(MP_ENUM_CACHE, enumClassType, MybatisBaseEnumTypeHandler::isMpEnums))) {
|
||||
delegate = new MybatisBaseEnumTypeHandler<>(enumClassType);
|
||||
} else {
|
||||
delegate = getInstance(enumClassType, defaultEnumTypeHandler);
|
||||
}
|
||||
}
|
||||
|
||||
public static void setDefaultEnumTypeHandler(Class<? extends TypeHandler> defaultEnumTypeHandler) {
|
||||
if (defaultEnumTypeHandler != null && !MybatisBaseEnumTypeHandler.class
|
||||
.isAssignableFrom(defaultEnumTypeHandler)) {
|
||||
CompositeBaseEnumTypeHandler.defaultEnumTypeHandler = defaultEnumTypeHandler;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
|
||||
delegate.setParameter(ps, i, parameter, jdbcType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getResult(ResultSet rs, String columnName) throws SQLException {
|
||||
return delegate.getResult(rs, columnName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return delegate.getResult(rs, columnIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return delegate.getResult(cs, columnIndex);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> TypeHandler<T> getInstance(Class<?> javaTypeClass, Class<?> typeHandlerClass) {
|
||||
if (javaTypeClass != null) {
|
||||
try {
|
||||
Constructor<?> c = typeHandlerClass.getConstructor(Class.class);
|
||||
return (TypeHandler<T>)c.newInstance(javaTypeClass);
|
||||
} catch (NoSuchMethodException ignored) {
|
||||
// ignored
|
||||
} catch (Exception e) {
|
||||
throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Constructor<?> c = typeHandlerClass.getConstructor();
|
||||
return (TypeHandler<T>)c.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -43,10 +43,11 @@ import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 自定义枚举属性转换器
|
||||
* 枚举类型处理器(扩展 BaseEnum 支持)
|
||||
*
|
||||
* @author hubin
|
||||
* @author hubin(<a href="https://gitee.com/baomidou/mybatis-plus">MyBatis Plus</a>)
|
||||
* @author Charles7c
|
||||
* @see com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
|
||||
* @since 2.4.0
|
||||
*/
|
||||
public class MybatisBaseEnumTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {
|
||||
|
Reference in New Issue
Block a user