• MybatisPlus中Enum的使用(MybatisEnumTypeHandler)及遇到的问题


    1.基本使用

    简单的使用可以参照官网的文档,三步走

    第一步,要用Enum就要先创建一个,我这里随便写了个,要注意的是两个注解

    @EnumValue 这个是mybatisplus的注解,代表如果使用Enum作为实体类中字段的类型,那会找到对应Enum中标识为@EnumValue的字段存入数据库

    @JsonValue 这个是jackson的注解,是把此注解标记的值返回给前端。如果用gson或者fastjson也会有对应的方式,此处不赘述

    这里也可以实现IEnum接口,效果是一样的,看具体情况

    1. import com.baomidou.mybatisplus.annotation.EnumValue;
    2. import com.fasterxml.jackson.annotation.JsonValue;
    3. import lombok.Getter;
    4. import lombok.RequiredArgsConstructor;
    5. @Getter
    6. @RequiredArgsConstructor
    7. public enum TemplateEnum {
    8.    /**
    9.     * 通用模板
    10.     */
    11.    GENERIC("通用模板"),
    12.    /**
    13.     * 专用模板
    14.     */
    15.    DEDICATED("专用模板");
    16.    @EnumValue
    17.    @JsonValue
    18.    private final String desc;
    19. }

    第二步,实体类要用使用对应的枚举类

    可以看到templateType字段是TemplateEnum类型的

    1. import com.baomidou.mybatisplus.annotation.TableField;
    2. import com.baomidou.mybatisplus.annotation.TableId;
    3. import com.baomidou.mybatisplus.annotation.TableName;
    4. import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
    5. import io.swagger.annotations.ApiModel;
    6. import io.swagger.annotations.ApiModelProperty;
    7. import lombok.Data;
    8. import org.apache.ibatis.type.JdbcType;
    9. import java.io.Serializable;
    10. import java.time.LocalDateTime;
    11. @TableName(value = "template")
    12. @Data
    13. @ApiModel(value = "Template对象", description = "模板表")
    14. public class Template implements Serializable {
    15.    private static final long serialVersionUID = 1L;
    16.    @ApiModelProperty("模板编码")
    17.    @TableId
    18.    private String templateCode;
    19.    
    20.    @ApiModelProperty("模板类型")
    21.    private TemplateEnum templateType;
    22. }

    第三步

    配置扫描路径,就是enum所在的包,也可以具体到某个类。用;分割

    目前我使用的是mybatisplus3.5.1,默认的typeHandler是MybatisEnumTypeHandler,所以这里也可以不用设置

    1. mybatis-plus:
    2.    # 支持统配符 * 或者 ; 分割
    3.   typeEnumsPackage: com.baomidou.springboot.entity.enums

    2.我常用的方式

    上面的方法也还有别的实现方式,再不改变开发依赖的情况下,能变动的就是扫描方式呗,对mybatisplus来说配置可以做的事情注入式也可以实现。

    上面的第三步不用了 ,然后又分两种情况,BaseMapper方式和Mapper.xml方式,其实主要是看你的sql语句在哪

    BaseMapper就是说使用mybatisplus带的IService或者BaseMapper实现好的方法

    我们都知道如果要使用selectById方法,要在实体类使用@TableId注解才行,可以说实体类和BaseMapper是绑定的。

    1. @TableName(value = "template",autoResultMap = true)
    2. @Data
    3. @ApiModel(value = "Template对象", description = "模板表")
    4. public class Template implements Serializable {
    5.    private static final long serialVersionUID = 1L;
    6.    @ApiModelProperty("模板编码")
    7.    @TableId
    8.    private String templateCode;
    9.    
    10.    @ApiModelProperty("模板类型")
    11.    @TableField(typeHandler = MybatisEnumTypeHandler.class)
    12.    private TemplateEnum templateType;
    13. }
     
    

    另外一种就是在xml中加入对应的typeHandler

    1. <resultMap id="base" type="com.xxx.Template">
    2.        <result column="template_code" property="templateCode"/>
    3.        <result column="template_type" property="templateType" typeHandler="com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler"/>
    4. resultMap>

    3.使用中遇到的一些问题

    请注意,当项目中同时使用BaseMapper方式和Mapper.xml方式并且都有注入对应typeHandler的时候是可以的,但是不能再使用配置文件扫描整个包,这样会和字段上定义的typeHandler产生冲突报错。

    按照上述方式2进行配置,可能会出现插入时javaType不匹配报错

    在进行bean注入的时候,我们要创建MybatisEnumTypeHandler,需要用TypeHandlerRegistry类中的getInstance反射进行创建,这里有个重要的参数就是javaType,这个参数可以在实体类或xml中进行配置。

    下面是TypeHandlerRegistry部分代码,其中javaTypeClass会根据是否配置javaType进行变化,如果javaType = true,那会按照实体类或者xml对应字段的java类型获取,如果javaType = false,那这里有可能是Object或者null,对于MybatisEnumTypeHandler来说应该是Object(我调试的时候是这样)

    1. public TypeHandler getInstance(Class javaTypeClass, Class typeHandlerClass) {
    2.        Constructor c;
    3.        if (javaTypeClass != null) {
    4.            try {
    5.                c = typeHandlerClass.getConstructor(Class.class);
    6.                return (TypeHandler)c.newInstance(javaTypeClass);
    7.           } catch (NoSuchMethodException var5) {
    8.           } catch (Exception var6) {
    9.                throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, var6);
    10.           }
    11.       }
    12.        try {
    13.            c = typeHandlerClass.getConstructor();
    14.            return (TypeHandler)c.newInstance();
    15.       } catch (Exception var4) {
    16.            throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, var4);
    17.       }
    18.   }

    如果注册是Object,那么对于MybatisEnumTypeHandler的构造函数来说

    1. public MybatisEnumTypeHandler(Class enumClassType) {
    2.    if (enumClassType == null) {
    3.        throw new IllegalArgumentException("Type argument cannot be null");
    4.   } else {
    5.        this.enumClassType = enumClassType;
    6.        MetaClass metaClass = MetaClass.forClass(enumClassType, REFLECTOR_FACTORY);
    7.        String name = "value";
    8.        if (!IEnum.class.isAssignableFrom(enumClassType)) {
    9.            name = (String)findEnumValueFieldName(this.enumClassType).orElseThrow(() -> {
    10.                return new IllegalArgumentException(String.format("Could not find @EnumValue in Class: %s.", this.enumClassType.getName()));
    11.           });
    12.       }
    13.        this.propertyType = ReflectionKit.resolvePrimitiveIfNecessary(metaClass.getGetterType(name));
    14.        this.getInvoker = metaClass.getGetInvoker(name);
    15.   }
    16. }

    propertyType接收到的就是Object,在SqlSourceBuilder创建参数映射的时候可能就会找不到对的参数类型

    1. private ParameterMapping buildParameterMapping(String content) {
    2.    Map propertiesMap = this.parseParameterMapping(content);
    3.    String property = (String)propertiesMap.get("property");
    4.    Class propertyType;
    5.    if (this.metaParameters.hasGetter(property)) {
    6.        propertyType = this.metaParameters.getGetterType(property);
    7.   } else if (this.typeHandlerRegistry.hasTypeHandler(this.parameterType)) {
    8.        propertyType = this.parameterType;
    9.   } else if (JdbcType.CURSOR.name().equals(propertiesMap.get("jdbcType"))) {
    10.        propertyType = ResultSet.class;
    11.   } else if (property != null && !Map.class.isAssignableFrom(this.parameterType)) {
    12.        MetaClass metaClass = MetaClass.forClass(this.parameterType, this.configuration.getReflectorFactory());
    13.        if (metaClass.hasGetter(property)) {
    14.            propertyType = metaClass.getGetterType(property);
    15.       } else {
    16.            propertyType = Object.class;
    17.       }
    18.   } else {
    19.        propertyType = Object.class;
    20.   }
    21.    Builder builder = new Builder(this.configuration, property, propertyType);
    22.    Class javaType = propertyType;
    23.    String typeHandlerAlias = null;
    24.    Iterator var8 = propertiesMap.entrySet().iterator();
    25.    while(var8.hasNext()) {
    26.        Entry entry = (Entry)var8.next();
    27.        String name = (String)entry.getKey();
    28.        String value = (String)entry.getValue();
    29.        if ("javaType".equals(name)) {
    30.            javaType = this.resolveClass(value);
    31.            builder.javaType(javaType);
    32.       } else if ("jdbcType".equals(name)) {
    33.            builder.jdbcType(this.resolveJdbcType(value));
    34.       } else if ("mode".equals(name)) {
    35.            builder.mode(this.resolveParameterMode(value));
    36.       } else if ("numericScale".equals(name)) {
    37.            builder.numericScale(Integer.valueOf(value));
    38.       } else if ("resultMap".equals(name)) {
    39.            builder.resultMapId(value);
    40.       } else if ("typeHandler".equals(name)) {
    41.            typeHandlerAlias = value;
    42.       } else if ("jdbcTypeName".equals(name)) {
    43.            builder.jdbcTypeName(value);
    44.       } else if (!"property".equals(name)) {
    45.            if ("expression".equals(name)) {
    46.                throw new BuilderException("Expression based parameters are not supported yet");
    47.           }
    48.            throw new BuilderException("An invalid property '" + name + "' was found in mapping #{" + content + "}. Valid properties are " + "javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName");
    49.       }
    50.   }
    51.    if (typeHandlerAlias != null) {
    52.        builder.typeHandler(this.resolveTypeHandler(javaType, typeHandlerAlias));
    53.   }
    54.    return builder.build();
    55. }

    所以如果使用MybatisEnumTypeHandler报错有关javatype,可以设置对应参数解决

    我得经验也不是很多,对于源码理解不深刻,如有错误还请各位大佬指出;

    上述问题解决方案也并不完美,只是希望能给大家提供一个解决思路。

  • 相关阅读:
    零基础入门金融风控之贷款违约预测挑战赛——简单实现
    Linux下发生几个字节内存泄露检测方法
    Roaring bitmaps算法
    Docker安装Prometheus + Grafana
    亿流量大考(4):自研ES+HBase+纯内存的高性能毫秒级查询引擎
    822 - Queue and A (UVA)
    ros1仿真导航机器人 navigation
    SOLR分组聚合的相关技巧
    可道云kodbox上传出错解决办法
    多级缓存的原理和实现
  • 原文地址:https://blog.csdn.net/yiyuzz/article/details/126121615