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的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-04-04
Spring中@RequestMapping、@RestController和Postman
本文介绍了Spring框架中常用的@RequestMapping和@RestController注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2024-10-10
springcloud + mybatis + seate集成示例
本文主要介绍了springcloud + mybatis + seate集成示例,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2021-06-06
Java弱键集合WeakHashMap及ConcurrentCache原理详解
这篇文章主要介绍了Java弱键集合WeakHashMap及ConcurrentCache原理详解,基于哈希表的Map接口实现,支持null键和值,但是WeakHashMap具有弱键,可用来实现缓存存储,在进行GC的时候会自动回收键值对,需要的朋友可以参考下2023-09-09
在SpringBoot中使用jwt实现token身份认证的实例代码
你还不会在SpringBoot中使用jwt实现token身份认证吗,本文小编就给大家详细的介绍一下在SpringBoot中使用jwt实现token身份认证的实例代码,感兴趣的同学可以自己动手试一试2023-09-09


最新评论