枚举使用
数据库存储字段为code 前端返回为msg 修改时需要传入code
枚举代码
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;@Getter
@AllArgsConstructor
public enum AuditStatus {UNREVIEWED("10","未提交审核"), // 未提交审核PENDING_FIRST_REVIEW("20","待初审"), // 待初审PENDING_FINAL_REVIEW("30","待终审"), // 待终审FIRST_REVIEW_REJECTED("40","初审退回"), // 初审退回FINAL_REVIEW_REJECTED("50","终审退回") ,// 终审退回APPROVED("60","已通过"); // 已通过@EnumValue@JsonValueprivate final String code;private final String msg;public static AuditStatus fromCode(String code) {return Arrays.asList(AuditStatus.values()).stream().filter(auditStatus -> auditStatus.code.equals(code)).findAny().orElseThrow(() -> new ServiceException(String.format("审核状态码%s错误!,请联系管理员", code)));}
}
实体类
public class BusQualification{@ApiModelProperty("审核状态 10,未提交审核 20,待初审 30,待终审 40,初审退回 50,终审退回 60,已通过 ")private QualificationAuditStatus qualificationAuditStatus;}
增加配置类
选择其一即可
1.全局配置
@Configuration
public class QualificationEnumConfig implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new Converter<String, QualificationAuditStatus>() {@Overridepublic QualificationAuditStatus convert(String source) {return QualificationAuditStatus.fromCode(source);}});}
}
2.部分配置
部分配置的时候注意 如果继承的有别的类 可能覆盖父类的这个方法
@InitBinderpublic void initBinderQualification(WebDataBinder binder) {// 仅为 QualificationAuditStatus 注册转换器binder.registerCustomEditor(QualificationAuditStatus.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String text) throws IllegalArgumentException {setValue(QualificationAuditStatus.fromCode(text));}});}