Remember a type conversion problem when using enumeration type in Mybatis Plus

Please refer to official documents for specific basic use methods: https://mp.baomidou.com/guide/enum.html , here just want to list some problems encountered and solutions. The mp version used in my system is 3.0.6 (easier to use after 3.1.0). I use two enumeration types AuditMethodEnum and enablestateenum in an entity object. The code is as follows:

@Data
@TableName(value = "audit_item")
@Accessors(chain = true)
public class AuditItem {
    @TableId(value = "id", type= IdType.AUTO)
    private Integer id;
    private String name;
    private AuditMethodEnum auditMethod;
    private String description;
    private Integer companyId;
    private Integer operatorId;
    private EnableStatusEnum status;
    private LocalDateTime createTime;
}

Among them, AuditMethodEnum is as follows:

public enum AuditMethodEnum implements IEnum {
    /**
     * Bluetooth
     */
    BLUETOOTH(1, "bluetooth"),
    /**
     * QR code
     */
    QRCODE(2, "qrcode"),
    /**
     * NFC
     */
    NFC(3, "nfc"),
    ;

    @Getter
    @Setter
    private Integer value;

    @Getter
    @Setter
    private String description;

    AuditMethodEnum(final Integer value, String description) {
        this.value = value;
        this.description = description;
    }

    @Override
    public Integer getValue() {
        return this.value;
    }

    @JsonValue
    public String getDescription() {
        return MessageUtils.get("audit.method." + this.description);
    }
}

Note: MessageUtils.get() is used for multi language processing.

Another enumeration, enablestateenum, is as follows:

public enum EnableStatusEnum implements IEnum{
    /**
     * available
     */
    ENABLED(1, "enabled"),

    /**
     * Prohibit
     */
    DISABlED(0, "disabled");

    @Getter
    @Setter
    private Integer value;

    @Getter
    @Setter
    private String description;

    EnableStatusEnum(final Integer value, String  description) {
        this.value = value;
        this.description = description;
    }

    @Override
    public Integer getValue() {
        return this.value;
    }
    @JsonValue
    public String getDescription(){
        return MessageUtils.get("enable.status." + this.description);
    }
}

However, in the result obtained, the two values of auditMethod and status are null. Finally, it is found that because the type of status is tinyint(1), the value taken from the table in com.baomidou.mybatisplus.extension.handlers.EnumTypeHandler is automatically treated as a Boolean type, resulting in the two enums being null. Just change the type of status to the default tinyint(4).

Tags: Programming QRCode

Posted on Sat, 09 May 2020 09:15:25 -0700 by shehroz