实体类:
@TableName("activity_info")
public class ActivityInfo extends BaseEntity {
@TableField("activity_name")
private String activityName;
@TableField("activity_type")
private ActivityType activityType;
}
枚举类ActivityType:
@Getter
public enum ActivityType {
FULL_REDUCTION(1, "满减"),
FULL_DISCOUNT(2, "满量打折");
@EnumValue
private Integer code;
private String comment ;
ActivityType(Integer code, String comment ){
this.code=code;
this.comment=comment;
}
}
数据库表activity_info中activity_type字段为整型,存储1和2。但是对应的实体类的activityType字段却为枚举类ActivityType类型,如果我们通过mybatis-plus直接查询会导致字段映射失败,提示整型字段无法映射为枚举类。我们只需要在配置文件中进行如下配置,同时在枚举类中在数据表中对应映射字段添加@EnumValue注解:
#配置枚举 支持通配符 * 或者 ; 分割。指定枚举类所在的包
mybatis-plus:
type-enums-package: com.demo.mybatisplus.enum
configuration:
default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
#handler配置可以省略不写,默认配置就是上面这个Handler