Mybatis-plus学习之@EnumValue注解学习

前言

在实际开发中,对于一些状态类的字段,我们通常使用的是枚举,而保存到数据库时,我们是用的枚举的某一个属性进行保存的,这里就会有一个问题,在PO类中,如果我们直接使用枚举类型去映射数据库的对应字段保存时,往往就会因为类型不匹配导致映射失败,如果要解决这个问题,办法有很多种,Mybatis-plus提供了一种解决办法,就是使用@EnumValue注解,这里我们就使用这种方式。

Maven引入的依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Demo

PO类
@Data
@TableName(value = "urge_reduce_rule")
public class ReduceRule {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField(value = "charge_category")
    private ChargeCategoryEnum chargeCategoryEnum;

    @TableField(value = "name")
    private String name;
}
枚举类(@EnumValue注解就用在这里)
@Getter
public enum ChargeCategoryEnum {

    CHARGE("CHARGE",1,"基本费"),
    PENALTY("PENALTY",2,"违约金");

    private String code;
    
    @EnumValue  //在需要保存到数据库的值上面加上注解
    private Integer value;
    
    private String text;

    public String getCode() {
        return code;
    }

    ChargeCategoryEnum(String code, Integer value, String text) {
        this.code = code;
        this.value = value;
        this.text = text;
    }
}
mapper类
@Mapper
public interface ReduceRuleMapper extends BaseMapper<ReduceRule> {
}
配置文件
#配置枚举 支持通配符 * 或者 ; 分割。指定枚举类所在的包
mybatis-plus:
  type-enums-package: com.demo.mybatisplus.enum 
  configuration:
    default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
  #handler配置可以省略不写,默认配置就是上面这个Handler
测试代码
@SpringBootTest(classes = DemoApplication.class)
class DemoApplicationTests {
    @Resource
    private ReduceRuleMapper reduceRuleMapper;

    @Test
    void test1(){
        ReduceRule reduceRule = new ReduceRule();
        reduceRule.setName("名字");
        reduceRule.setChargeCategoryEnum(ChargeCategoryEnum.PENALTY);
        reduceRuleMapper.insert(reduceRule);
    }
    
    @Test
    void test2(){
        ReduceRule reduceRule = reduceRuleMapper.selectById(32L);
        System.out.println(reduceRule);
    }
}

拓展

如果返回给前端不希望直接将枚举返回的话,需要在枚举类上加上 @JsonValue 注解

@Getter
public enum ChargeCategoryEnum {

    CHARGE("CHARGE",1,"基本费"),
    PENALTY("PENALTY",2,"违约金");

    private String code;
    
    @EnumValue  //在需要保存到数据库的值上面加上注解
    private Integer value;
    
    @JsonValue    //需要在前端展示哪个值就在哪个属性上加上该注解
    private String text;

    public String getCode() {
        return code;
    }

    ChargeCategoryEnum(String code, Integer value, String text) {
        this.code = code;
        this.value = value;
        this.text = text;
    }
}
  • 13
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus提供了一些常用的注解,可以帮助简化开发过程和提高开发效率。其中常用的注解包括: 1. @TableName注解:用于指定实体类对应的数据库表名。可以在实体类的类级别上使用,例如:@TableName("my_table")。 2. @TableId注解:用于指定实体类的主键字段。可以在主键字段上使用,例如:@TableId(value = "id", type = IdType.AUTO)。 3. @TableField注解:用于指定实体类的字段与数据库表字段的映射关系。可以在字段上使用,例如:@TableField("name")。 4. @Version注解:用于指定实体类的乐观锁字段。可以在乐观锁字段上使用,例如:@Version。 5. @EnumValue注解:用于指定实体类枚举字段的数据库存储值。可以在枚举字段上使用,例如:@EnumValue("1")。 这些注解可以帮助开发人员简化代码编写,减少了手动编写XML的工作量,提高了开发效率。通过使用这些注解,可以更加方便地进行数据库操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mybatis-plus常用注解](https://blog.csdn.net/m0_61682705/article/details/125348601)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis-Plus——详解常用注解](https://blog.csdn.net/Huang_ZX_259/article/details/122516135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值