diff --git a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/autoconfigure/MybatisPlusAutoConfiguration.java b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/autoconfigure/MybatisPlusAutoConfiguration.java index e72d8ec7..701d4d91 100644 --- a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/autoconfigure/MybatisPlusAutoConfiguration.java +++ b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/autoconfigure/MybatisPlusAutoConfiguration.java @@ -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); } /** diff --git a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/CompositeBaseEnumTypeHandler.java b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/CompositeBaseEnumTypeHandler.java new file mode 100644 index 00000000..368f6b15 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/CompositeBaseEnumTypeHandler.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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(MyBatis Plus) + * @author Charles7c + * @see com.baomidou.mybatisplus.core.handlers.CompositeEnumTypeHandler + * @since 2.7.3 + */ +public class CompositeBaseEnumTypeHandler> implements TypeHandler { + + private static final Map, Boolean> MP_ENUM_CACHE = new ConcurrentHashMap<>(); + private static Class defaultEnumTypeHandler = EnumTypeHandler.class; + private final TypeHandler delegate; + + public CompositeBaseEnumTypeHandler(Class 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 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 TypeHandler getInstance(Class javaTypeClass, Class typeHandlerClass) { + if (javaTypeClass != null) { + try { + Constructor c = typeHandlerClass.getConstructor(Class.class); + return (TypeHandler)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)c.newInstance(); + } catch (Exception e) { + throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e); + } + } +} \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/MybatisBaseEnumTypeHandler.java b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/MybatisBaseEnumTypeHandler.java index 916679c1..39d2b18e 100644 --- a/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/MybatisBaseEnumTypeHandler.java +++ b/continew-starter-data/continew-starter-data-mp/src/main/java/top/continew/starter/data/mp/handler/MybatisBaseEnumTypeHandler.java @@ -43,10 +43,11 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; /** - * 自定义枚举属性转换器 + * 枚举类型处理器(扩展 BaseEnum 支持) * - * @author hubin + * @author hubin(MyBatis Plus) * @author Charles7c + * @see com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler * @since 2.4.0 */ public class MybatisBaseEnumTypeHandler> extends BaseTypeHandler {