Mybatis-plus中的@EnumValue注解使用详解

 更新时间:2024年02月02日 09:12:00   作者:真正的大师学徒的心  
这篇文章主要介绍了Mybatis-plus中的@EnumValue注解使用详解,在PO类中,如果我们直接使用枚举类型去映射数据库的对应字段保存时,往往就会因为类型不匹配导致映射失败,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;
    }
}

到此这篇关于Mybatis-plus中的@EnumValue注解使用详解的文章就介绍到这了,更多相关@EnumValue注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用@Autowired注解警告Field injection is not recommended的解决

    使用@Autowired注解警告Field injection is not recommended的解决

    这篇文章主要介绍了使用@Autowired注解警告Field injection is not recommended的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 详解Spring Boot Profiles 配置和使用

    详解Spring Boot Profiles 配置和使用

    本篇文章主要介绍了详解Spring Boot Profiles 配置和使用,具有一定的参考价值,有兴趣的可以了解一下
    2017-06-06
  • Spring中@RequestMapping、@RestController和Postman

    Spring中@RequestMapping、@RestController和Postman

    本文介绍了Spring框架中常用的@RequestMapping和@RestController注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-10-10
  • springcloud + mybatis + seate集成示例

    springcloud + mybatis + seate集成示例

    本文主要介绍了springcloud + mybatis + seate集成示例,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06
  • Java弱键集合WeakHashMap及ConcurrentCache原理详解

    Java弱键集合WeakHashMap及ConcurrentCache原理详解

    这篇文章主要介绍了Java弱键集合WeakHashMap及ConcurrentCache原理详解,基于哈希表的Map接口实现,支持null键和值,但是WeakHashMap具有弱键,可用来实现缓存存储,在进行GC的时候会自动回收键值对,需要的朋友可以参考下
    2023-09-09
  • springboot启动流程过程

    springboot启动流程过程

    Spring Boot 简化了 Spring 框架的使用,通过创建 `SpringApplication` 对象,判断应用类型并设置初始化器和监听器,在 `run` 方法中,读取配置并加载到 `Environment` 中,通过 Spring 事件机制和 `EnvironmentPostProcessor` 处理配置
    2025-02-02
  • JDBC如何访问MySQL数据库,并增删查改

    JDBC如何访问MySQL数据库,并增删查改

    这篇文章主要介绍了JDBC如何访问MySQL数据库,帮助大家更好的理解和学习java与MySQL,感兴趣的朋友可以了解下
    2020-08-08
  • Java中PreparedStatement的用法解析

    Java中PreparedStatement的用法解析

    这篇文章主要介绍了Java中PreparedStatement的用法解析,在JDBC应用中,PreparedStatement是一种比Statement更好的选择,PreparedStatement可以通过使用参数化查询来避免SQL注入攻击,并且可以提高查询性能,需要的朋友可以参考下
    2023-09-09
  • 在SpringBoot中使用jwt实现token身份认证的实例代码

    在SpringBoot中使用jwt实现token身份认证的实例代码

    你还不会在SpringBoot中使用jwt实现token身份认证吗,本文小编就给大家详细的介绍一下在SpringBoot中使用jwt实现token身份认证的实例代码,感兴趣的同学可以自己动手试一试
    2023-09-09
  • SpringBoot添加License的多种方式

    SpringBoot添加License的多种方式

    License指的是版权许可证,当我们开发完系统后,如果不想让用户一直白嫖使用,比如说按时间续费,License的作用就有了。我们可以给系统指定License的有效期,控制系统的可用时间。
    2021-06-06

最新评论