Mybatis-Plus通用枚举

2022-08-19 09:19:54

Mybatis-Plus通用枚举

添加通用枚举的步骤:

  1. 在枚举类中要添加到数据库中的属性前添加@EnumValue
  2. 在配置文件中扫描枚举所在的位置

在配置文件中配置枚举位置

mybatis-plus:type-enums-package: com.example.wx_test.enums

定义枚举类

packagecom.example.wx_test.enums;importcom.baomidou.mybatisplus.annotation.EnumValue;importlombok.Getter;@GetterpublicenumStatusEnum{ONTHEJOB(0,"在职"),QUIT(1,"离职"),VACATION(2,"休假");@EnumValue//将注解所标识的属性的值存储在数据库中privateInteger status;privateString statusName;StatusEnum(Integer status,String statusName){this.status= status;this.statusName= statusName;}}

在实体类中添加相应的属性

packagecom.example.wx_test.entity;importcom.baomidou.mybatisplus.annotation.TableField;importcom.baomidou.mybatisplus.annotation.TableName;importcom.example.wx_test.enums.StatusEnum;importlombok.Data;@TableName("t_employee")@DatapublicclassEmployee{privateInteger id;privateString name;privateString gender;privateString email;privateString phoneNumber;@TableField(value="departmentId")privateInteger departmentId;privateInteger salary;//枚举类-员工状态privateStatusEnum status;}

测试类

packagecom.example.wx_test;importcom.example.wx_test.entity.Employee;importcom.example.wx_test.enums.StatusEnum;importcom.example.wx_test.mapper.EmployeeMapper;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublicclassEmployeeEnumTest{@AutowiredprivateEmployeeMapper employeeMapper;@Testpublicvoidtest(){Employee employee=newEmployee();
        employee.setName("王小刀");
        employee.setPhoneNumber("12365445611");
        employee.setEmail("999@qq.com");
        employee.setGender("男");
        employee.setDepartmentId(2);
        employee.setSalary(9999);
        employee.setStatus(StatusEnum.ONTHEJOB);int result= employeeMapper.insert(employee);System.out.println(result);}}

参考资料:

  1. https://www.bilibili.com/video/BV12R4y157Be?p=49
  • 作者:寒叶飘逸_
  • 原文链接:https://blog.csdn.net/Snakewood/article/details/124587881
    更新时间:2022-08-19 09:19:54