Mybatis枚举类型转换

Mybatis枚举类型转换

类型转换器源码分析

在Mybatis的TypeHandlerRegistry中,添加了常用的类转换器,其中默认的枚举类型转换器是EnumTypeHandler。

cpp 复制代码
public final class TypeHandlerRegistry {
  ....
  
  public TypeHandlerRegistry(Configuration configuration) {
    this.unknownTypeHandler = new UnknownTypeHandler(configuration);

    register(Boolean.class, new BooleanTypeHandler());
    register(boolean.class, new BooleanTypeHandler());
    register(JdbcType.BOOLEAN, new BooleanTypeHandler());
    register(JdbcType.BIT, new BooleanTypeHandler());
    ...

EnumTypeHandler.java,默认使用的是枚举的名称设置参数和转换枚举类型。

cpp 复制代码
public EnumTypeHandler(Class<E> type) {
    if (type == null) {
      throw new IllegalArgumentException("Type argument cannot be null");
    }
    this.type = type;
  }

  @Override
  public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
    if (jdbcType == null) {
      ps.setString(i, parameter.name());
    } else {
      ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589
    }
  }

  @Override
  public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
    String s = rs.getString(columnName);
    return s == null ? null : Enum.valueOf(type, s);
    ...

以下代码展示了如何为确定枚举类型的类型转换器。

  1. 首先直接获取对应类型的类型型转换器,包括原始类型,包括raw type(原始类型,对应Class),parameterized types(参数化类型), array types(数组类型),这是最精确的匹配。
  2. 如果是Class类型且枚举类型是其接口或父类,如果是匿名类,寻找其父类的类型转换器,否则再不断递归寻找该枚举类的接口类型转换器,如果没有找到,直接利用反射获defaultEnumHandler的的对象,专门用于处理该枚举类型,在图中是GroupStatusEnum。
  3. 如果不是枚举类型,则再尝试其父类。

getInstance方法如下

cpp 复制代码
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);
    }
  }

自定义通用枚举类型

  1. 为项目中所有枚举类型定一个接口

    java 复制代码
    public interface BaseEnum {
        Integer getValue();
    }
  2. 枚举类实现该接口

    java 复制代码
    @AllArgsConstructor
    @Getter
    public enum GroupStatusEnum implements BaseEnum {
        DISBANDED("已解散", 0),
        NORMAL("正常", 1);
        private final String desc;
        private final Integer value;
    }
  3. 定义通用枚举类型转换器

    cpp 复制代码
    public class GenericEnumHandler<E extends BaseEnum> implements TypeHandler<E> {
    
        private final Map<Integer, E> map = new HashMap<>();
    
        public GenericEnumHandler(Class<E> clazz) {
            E[] constants = clazz.getEnumConstants();
            for (E constant : constants) {
                map.put(constant.getValue(), constant);
            }
        }
    
        @Override
        public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
            ps.setInt(i, parameter.getValue());
        }
    
        @Override
        public E getResult(ResultSet rs, String columnName) throws SQLException {
            return map.get(rs.getInt(columnName));
        }
    
        @Override
        public E getResult(ResultSet rs, int columnIndex) throws SQLException {
            return map.get(rs.getInt(columnIndex));
        }
    
        @Override
        public E getResult(CallableStatement cs, int columnIndex) throws SQLException {
            return map.get(cs.getInt(columnIndex));
        }
    }

按照上述源码分析流程,当GroupStatusEnum第一次需要转化数据库的int时,mybatis去寻找类型转换器。

  • 我们没有为这种类型定义专门的类型转换器(TypeHandler<GroupStatusEnum>)。
  • 该类不是内部类。
  • 该类实现了BaseEnum接口,但是我们没有为BaseEnum定义类型转换器。
  • 使用该类和默认的枚举类利用反射构造一个对象处理该枚举,即new GenericEnumTypeHandler(GroupStatusEnum.class)。

使用方法

yaml 复制代码
mybatis:
  configuration:
    local-cache-scope: statement
    jdbc-type-for-null: null
    use-generated-keys: true
    cache-enabled: false
    map-underscore-to-camel-case: true
    default-enum-type-handler: com.windcf.easychatjava.typehandler.GenericEnumHandler
  mapper-locations: classpath:/mappers/**/*.xml
#  type-handlers-package: com.windcf.easychatjava.typehandler
相关推荐
shiming88794 分钟前
Python数据分析与可视化
开发语言·python·数据分析
kid_sup11 分钟前
C语言错题本
c语言·开发语言
白总Server37 分钟前
MySQL在大数据场景应用
大数据·开发语言·数据库·后端·mysql·golang·php
c语言鹌鹑蛋39 分钟前
C++进阶 --- 多继承中的虚表问题
开发语言·c++
姑苏老陈44 分钟前
【Python基础】Python文件处理
开发语言·python·python文件操作
luoluoal1 小时前
java项目之企业级工位管理系统源码(springboot)
java·开发语言·spring boot
ch_s_t1 小时前
新峰商城之购物车(一)
java·开发语言
蜜桃小阿雯1 小时前
JAVA开源项目 校园美食分享平台 计算机毕业设计
java·jvm·spring boot·spring cloud·intellij-idea·美食
黄昏_1 小时前
苍穹外卖Day01-2
java·spring
努力的八爪鱼1 小时前
记录工作中遇到的问题(持续更新~)
java