JAVA枚举使用总结

背景: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。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值