创建案例
日志的操作类型:数据库存储类型code,前端展示类型描述
一、创建枚举
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
public enum OptBehaviorEnum {
NOT(0,"未知"),
ADD(10,"新增"),
UPDATE(20,"修改"),
SELECT(30,"查询"),
DELETE(40,"删除"),
LOGIN(100,"登录"),
LOGOUT(101,"登出"),
;
@Getter
@EnumValue
private final int code;
@Getter
@JsonValue
private final String msg;
OptBehaviorEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
在需要存储数据库的属性上添加 @EnumValue
注解,在需要前端展示的属性上添加 @JsonValue
注解
二、application.yml里添加mybatis-plus配置
mybatis-plus:
type-enums-package: com.guod.admin.enums
# 默认
# configuration:
# default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
三、设置entity类型为OptBehaviorEnum
/**
* 操作行为
*/
@ApiModelProperty(value = "操作行为")
private OptBehaviorEnum optBehavior;
四、测试
到此基本就完成了整个流程,接下来做一个简单的测试:
...
optLog.setOptBehavior(OptBehaviorEnum.UPDATE);
optLog.setOptBehavior(OptBehaviorEnum.SELECT);
optLog.setOptBehavior(OptBehaviorEnum.DELETE);
...
数据库保存的值:
ID | ... | opt_behavior |
---|---|---|
1 | ... | 20 |
2 | ... | 30 |
3 | ... | 40 |
前端查询的值:
[ {"id":"1","optBehavior":"修改"}, {"id":"2","optBehavior":"查询"}, {"id":"3","optBehavior":"删除"}]