浅谈MyBatisPlus注解@EnumValue避坑
作者:咖啡Beans
本文介绍如何使用MyBatisPlus的注解@EnumValue声明枚举类,同时指出需要避开的几个常见坑点,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
摘要
本文介绍如何使用MyBatisPlus的注解@EnumValue声明枚举类,同时指出需要避开的几个常见坑点。
认识注解
@EnumValue会自动将数据库中枚举类型(如tinyint)字段映射成Java对象枚举字段类型,同时也能将Java对象枚举类型字段的值映射成数据库中的枚举类型字段值。
应用场景
- 通过枚举统一管理单据状态,如订单状态(0: 待支付, 1: 已支付, 2: 已取消)
注意事项
- 枚举字段的默认值存在隐式分配:未显式指定枚举值时,默认从 0 开始递增,可能与数据库实际值冲突。
- 自定义映射逻辑缺失:未实现 TypeHandler 或未覆盖 getDescByCode() 方法,导致反向映射失败。
- 字段类型匹配:@EnumValue标记的枚举类属性的类型要和数据库字段的类型对应(如int对应TINYINT),否则在查询数据的时候无法转化为枚举类型,并显示为null。
代码示例
1)构建数据表结构[mysql]
CREATE TABLE `task` ( `id` BIGINT PRIMARY KEY AUTO_INCREMENT, `work_type` TINYINT NOT NULL, -- 映射到 WorkType 枚举 `description` VARCHAR(255) );
2)导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency>
3)定义实体对象
import com.baomidou.mybatisplus.annotation.*; import lombok.Data; @Data @TableName("task") public class Task { @TableId(type = IdType.AUTO) private Long id; // 数据库字段为 TINYINT,映射到 WorkType 枚举 @EnumValue @TableField("work_type") private WorkType workType; @TableField("description") private String description; }
4)定义枚举类
import com.baomidou.mybatisplus.annotation.EnumValue; //显式指定数值 public enum WorkType { DEV(0, "开发"), TEST(1, "测试"), IT(2, "运维"); @EnumValue private final int workCode; //数据库字段类型为TINYINT private final String workDec; WorkType(int workCode, String workDec) { this.workCode = workCode; this.workDec = workDec; } public String getWorkDec() { return workDec; } //通过数值获取文字描述 public static WorkType getDescByCode(int value) { for (WorkType workType : WorkType.values()) { if (workType.workCode == value) { return workType; } } return null; } }
5)使用MyBatisplus进行增删改查
import lombok.extern.slf4j.Slf4j; import org.coffeebeans.enumvalue.Task; import org.coffeebeans.enumvalue.TaskService; import org.coffeebeans.enumvalue.WorkType; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * <li>ClassName: org.org.coffeebeans.EnumValueTest </li> * <li>Author: OakWang </li> */ @Slf4j @SpringBootTest public class EnumValueTest { @Autowired private TaskService taskService; @Test void test1() { // 插入操作 Task task = new Task(); task.setWorkType(WorkType.IT); // Java 枚举 → 数据库 TINYINT task.setDescription("运维"); taskService.save(task); /* 执行时间:8 ms,执行SQL:INSERT INTO task ( work_type, description ) VALUES ( 2, '运维' ) */ } @Test void test2() { // 查询操作 Task result = taskService.getById(1L); System.out.println(result.getWorkType().getWorkDec()); /* 执行时间:10 ms,执行SQL:SELECT id,work_type AS workType,description FROM task WHERE id=1 运维 */ } @Test void test3() { // 查询操作 Task result = taskService.getById(1L); // 更新操作 result.setWorkType(WorkType.DEV); task.setDescription("开发"); taskService.updateById(result); /* 执行时间:10 ms,执行SQL:SELECT id,work_type AS workType,description FROM task WHERE id=1 执行时间:5 ms,执行SQL:UPDATE task SET work_type=0, description='开发' WHERE id=1 */ } }
总结
以上我们了解了MyBatisPlus中的注解@EnumValue可以巧妙灵活地自动映射对象字段和数据库表字段,使用时需要显式指定数值、匹配字段类型、合理自定义映射逻辑。
到此这篇关于浅谈MyBatisPlus注解@EnumValue避坑的文章就介绍到这了,更多相关MyBatisPlus @EnumValue内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!