jpa转换枚举、转换以及入库数据、状态值发生变化可以不影响前端
import com.peony.common.enums.PlanStatus; import org.apache.poi.ss.formula.functions.T; import javax.persistence.AttributeConverter; import javax.persistence.Converter; @Converter(autoApply = true) public class PlanStatusConverter implements AttributeConverter<PlanStatus,String> { @Override public String convertToDatabaseColumn(PlanStatus attribute) { return attribute.getCode(); } @Override public PlanStatus convertToEntityAttribute(String dbData) { return PlanStatus.valueOf(dbData); } }
枚举
S_TO_POSTPONE("14", "延期待审核"), /** * S_CHANGE_THE_DATE("11", "改期"), */ S_TO_CHANGE_THE_DATE("10", "改期待审核"), ; private final String code; private final String description; public PlanStatus valueOfCode(String code) { for (PlanStatus value : values()) { if (value.code.equals(code)) { return value; } } return null; }
po类
这样就可以查询和写入自动转换了 、
注意:
@Converter(autoApply = true)这个注解只有在同一个包下才生效
处理单个可以在字段上添加
@Convert(converter = SyslogStartConverter.class)