1.基本使用
简单的使用可以参照官网的文档,三步走
第一步,要用Enum就要先创建一个,我这里随便写了个,要注意的是两个注解
@EnumValue 这个是mybatisplus的注解,代表如果使用Enum作为实体类中字段的类型,那会找到对应Enum中标识为@EnumValue的字段存入数据库
@JsonValue 这个是jackson的注解,是把此注解标记的值返回给前端。如果用gson或者fastjson也会有对应的方式,此处不赘述
这里也可以实现IEnum接口,效果是一样的,看具体情况
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum TemplateEnum {
/**
* 通用模板
*/
GENERIC("通用模板"),
/**
* 专用模板
*/
DEDICATED("专用模板");
@EnumValue
@JsonValue
private final String desc;
}
第二步,实体类要用使用对应的枚举类
可以看到templateType字段是TemplateEnum类型的
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.ibatis.type.JdbcType;
import java.io.Serializable;
import java.time.LocalDateTime;
@TableName(value = "template")
@Data
@ApiModel(value = "Template对象", description = "模板表")
public class Template implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("模板编码")
@TableId
private String templateCode;
@ApiModelProperty("模板类型")
private TemplateEnum templateType;
}
第三步
配置扫描路径,就是enum所在的包,也可以具体到某个类。用;分割
目前我使用的是mybatisplus3.5.1,默认的typeHandler是MybatisEnumTypeHandler,所以这里也可以不用设置
mybatis-plus:
# 支持统配符 * 或者 ; 分割
typeEnumsPackage: com.baomidou.springboot.entity.enums
2.我常用的方式
上面的方法也还有别的实现方式,再不改变开发依赖的情况下,能变动的就是扫描方式呗,对mybatisplus来说配置可以做的事情注入式也可以实现。
上面的第三步不用了 ,然后又分两种情况,BaseMapper方式和Mapper.xml方式,其实主要是看你的sql语句在哪
BaseMapper就是说使用mybatisplus带的IService或者BaseMapper实现好的方法
我们都知道如果要使用selectById方法,要在实体类使用@TableId注解才行,可以说实体类和BaseMapper是绑定的。
@TableName(value = "template",autoResultMap = true)
@Data
@ApiModel(value = "Template对象", description = "模板表")
public class Template implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("模板编码")
@TableId
private String templateCode;
@ApiModelProperty("模板类型")
@TableField(typeHandler = MybatisEnumTypeHandler.class)
private TemplateEnum templateType;
}
另外一种就是在xml中加入对应的typeHandler
<resultMap id="base" type="com.xxx.Template">
<result column="template_code" property="templateCode"/>
<result column="template_type" property="templateType" typeHandler="com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler"/>
</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(我调试的时候是这样)
public <T> TypeHandler<T> getInstance(Class<?> javaTypeClass, Class<?> typeHandlerClass) {
Constructor c;
if (javaTypeClass != null) {
try {
c = typeHandlerClass.getConstructor(Class.class);
return (TypeHandler)c.newInstance(javaTypeClass);
} catch (NoSuchMethodException var5) {
} catch (Exception var6) {
throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, var6);
}
}
try {
c = typeHandlerClass.getConstructor();
return (TypeHandler)c.newInstance();
} catch (Exception var4) {
throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, var4);
}
}
如果注册是Object,那么对于MybatisEnumTypeHandler的构造函数来说
public MybatisEnumTypeHandler(Class<E> enumClassType) {
if (enumClassType == null) {
throw new IllegalArgumentException("Type argument cannot be null");
} else {
this.enumClassType = enumClassType;
MetaClass metaClass = MetaClass.forClass(enumClassType, REFLECTOR_FACTORY);
String name = "value";
if (!IEnum.class.isAssignableFrom(enumClassType)) {
name = (String)findEnumValueFieldName(this.enumClassType).orElseThrow(() -> {
return new IllegalArgumentException(String.format("Could not find @EnumValue in Class: %s.", this.enumClassType.getName()));
});
}
this.propertyType = ReflectionKit.resolvePrimitiveIfNecessary(metaClass.getGetterType(name));
this.getInvoker = metaClass.getGetInvoker(name);
}
}
propertyType接收到的就是Object,在SqlSourceBuilder创建参数映射的时候可能就会找不到对的参数类型
private ParameterMapping buildParameterMapping(String content) {
Map<String, String> propertiesMap = this.parseParameterMapping(content);
String property = (String)propertiesMap.get("property");
Class propertyType;
if (this.metaParameters.hasGetter(property)) {
propertyType = this.metaParameters.getGetterType(property);
} else if (this.typeHandlerRegistry.hasTypeHandler(this.parameterType)) {
propertyType = this.parameterType;
} else if (JdbcType.CURSOR.name().equals(propertiesMap.get("jdbcType"))) {
propertyType = ResultSet.class;
} else if (property != null && !Map.class.isAssignableFrom(this.parameterType)) {
MetaClass metaClass = MetaClass.forClass(this.parameterType, this.configuration.getReflectorFactory());
if (metaClass.hasGetter(property)) {
propertyType = metaClass.getGetterType(property);
} else {
propertyType = Object.class;
}
} else {
propertyType = Object.class;
}
Builder builder = new Builder(this.configuration, property, propertyType);
Class<?> javaType = propertyType;
String typeHandlerAlias = null;
Iterator var8 = propertiesMap.entrySet().iterator();
while(var8.hasNext()) {
Entry<String, String> entry = (Entry)var8.next();
String name = (String)entry.getKey();
String value = (String)entry.getValue();
if ("javaType".equals(name)) {
javaType = this.resolveClass(value);
builder.javaType(javaType);
} else if ("jdbcType".equals(name)) {
builder.jdbcType(this.resolveJdbcType(value));
} else if ("mode".equals(name)) {
builder.mode(this.resolveParameterMode(value));
} else if ("numericScale".equals(name)) {
builder.numericScale(Integer.valueOf(value));
} else if ("resultMap".equals(name)) {
builder.resultMapId(value);
} else if ("typeHandler".equals(name)) {
typeHandlerAlias = value;
} else if ("jdbcTypeName".equals(name)) {
builder.jdbcTypeName(value);
} else if (!"property".equals(name)) {
if ("expression".equals(name)) {
throw new BuilderException("Expression based parameters are not supported yet");
}
throw new BuilderException("An invalid property '" + name + "' was found in mapping #{" + content + "}. Valid properties are " + "javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName");
}
}
if (typeHandlerAlias != null) {
builder.typeHandler(this.resolveTypeHandler(javaType, typeHandlerAlias));
}
return builder.build();
}
所以如果使用MybatisEnumTypeHandler报错有关javatype,可以设置对应参数解决
我得经验也不是很多,对于源码理解不深刻,如有错误还请各位大佬指出;
上述问题解决方案也并不完美,只是希望能给大家提供一个解决思路。