2

springboot~mybatis-plus枚举到数据库整型字段

 2 years ago
source link: https://www.cnblogs.com/lori/p/16083908.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
  • 数据库字段为int类型
  • 实体字段为了可读性强,使用枚举类型
  • 一般来说,数据库为varchar时,你的枚举元素直接会进行转换

字段为整型

  • 这些如果你是mybatis-plus,可以使用如下default-enum-type-handler参数开启@EnumValue特性,将每句的@EnumValue注释的字段添加到数据表字段中。
public enum ModuleType implements NameValueEnum {
  LOGIN(0, "登录"),
  LOGOUT(1, "登出");

  @EnumValue
  private Integer code;
  @JsonValue
  private String name;

  ModuleType(Integer code, String name) {
    this.code = code;
    this.name = name;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public Integer getValue() {
    return code;
  }
}

@Data
@Builder
public class OperatorLog {
  private String id;
  private String dataId;
  private String dataTitle;
  /**
   * 模块类型
   */
  private ModuleType moduleType;
  private String content;
  /**
   * 操作类型
   */
  private OperateType operateType;
}

mybatis-plus:
  default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK