背景:java实体属性用枚举类型,进行接口调用及数据库访问
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum WorkerOrderStatisticTypeEnum {
all_order_statistic("all", "统计师傅所有订单数"),
team_order_statistic("team", "统计师傅团队订单数"),
share("share", "");
@EnumValue
private final String value;
private final String desc;
}
POST请求
总结:请求参数statisticType可传递all_order_statistic、team_order_statistic等String类型的name,也可传递Integer类型的下标,all_order_statistic下标为0,team_order_statistic下标为1,依次往下递增,如果传递的name或下标不存在,则无法请求成功。
mybaits-plus入库
开启sql打印:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
方式一(默认):通过枚举的name映射入库,如:all_order_statistic、team_order_statistic。
方式二:配置枚举类型转换处理器,匹配下标
mybatis-plus:
configuration:
#枚举类型转换枚举
default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
枚举值为all_order_statistic时,数据库保存为0;枚举值为team_order_statistic时,数据库保存为1;枚举值为share时,数据库保存为2。
方式三(推荐):通过注解@EnumValue指定入库枚举值
配置注解扫描包
mybatis-plus:
type-enums-package: com.lbdj.user.service.common.enums
枚举类加注解@EnumValue
枚举值为all_order_statistic时,数据库保存为all;枚举值为team_order_statistic时,数据库保存为team;枚举值为share时,数据库保存为share。