定义枚举类
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatusEnum {
YES("0", "是"),
NO("1", "否");
private String name;
private String desc;
private StatusEnum(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
定义对象
@Data
@ApiModel(value = "用户对象", description = "用户对象")
public class User {
@ApiModelProperty(value = "״状态")
private StatusEnum status;
}
关键是mapper映射的时候定义类型
<resultMap id="UserResultMap" type="com.xuekao.model.User">
<result column="status" property="status" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
</resultMap>
如果定义为其他类型会出现 No enum constant com.xuekao.enums.StatusEnum.异常