MybaitisPlus枚举类使用

我们实体类中有枚举类型时候,

 @ApiModelProperty(value = "活动类型(满减、折扣)")
   @TableField("activity_type")
   @EnumValue
   private ActivityType activityType;
package com.atguigu.ssyx.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

@Getter
public enum ActivityType {

    FULL_REDUCTION(1,"满减"),
    FULL_DISCOUNT(2,"满量打折");

    @EnumValue
    private Integer code;
    private String comment ;

    ActivityType(Integer code, String comment ){
        this.code=code;
        this.comment=comment;
    }
    public static ActivityType fromCode(Integer code) {
        for (ActivityType type : ActivityType.values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }
        return null; // or throw exception if necessary
    }
}
package com.atguigu.ssyx.model.activity;

import com.atguigu.ssyx.enums.ActivityType;
import com.atguigu.ssyx.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * <p>
 * ActivityInfo
 * </p>
 *
 * @author qy
 */
@Data
@ApiModel(description = "ActivityInfo")
@TableName("activity_info")
public class ActivityInfo extends BaseEntity {
   
   private static final long serialVersionUID = 1L;
   
   @ApiModelProperty(value = "活动名称")
   @TableField("activity_name")
   private String activityName;

   @ApiModelProperty(value = "活动类型(满减、折扣)")
   @TableField("activity_type")
   @EnumValue
   private ActivityType activityType;

   @ApiModelProperty(value = "活动描述")
   @TableField("activity_desc")
   private String activityDesc;

   @ApiModelProperty(value = "开始时间")
   @TableField("start_time")
   @JsonFormat(pattern = "yyyy-MM-dd")
   private Date startTime;

   @ApiModelProperty(value = "结束时间")
   @TableField("end_time")
   @JsonFormat(pattern = "yyyy-MM-dd")
   private Date endTime;

   @ApiModelProperty(value = "创建时间")
   @TableField("create_time")
   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   private Date createTime;

   @TableField(exist = false)
   private String activityTypeString;
}
  • private ActivityType activityType; 这个是一个枚举类别.

  • 他从数据库接收到的值是1或者0,我们需要在resoures配置文件设置一个枚举类的包地址和 @EnumValue

  • 他会把code为1的枚举值给这个类对象.之后根据枚举的code,获得完整的

    page.getRecords().stream().forEach(item -> {
        item.setActivityTypeString(item.getActivityType().getComment());
    });
    

    之后可以使用getComment();获得code为1根据code,获得从而获得ActivityType.FULL_REDUCTION
    。后面可以item.getActivityType().getComment()。

获得符合枚举的messgae.

mybatis-plus:
  type-enums-package: com.atguigu.ssyx.enums
  1. 数据库表中有枚举字段,在实体类中该字段也可以用枚举类表示的.之后实体类在增加一个处理字段.拿到他的string值。
  2. 也可以继续使用interger。拿到数字就可以
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@追求卓越

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值