8.Mybatis-Plus通用枚举

8.Mybatis-Plus通用枚举

添加通用枚举的步骤:

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

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

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

定义枚举类

package com.example.wx_test.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

@Getter
public enum StatusEnum {
    ONTHEJOB(0,"在职"),
    QUIT(1,"离职"),
    VACATION(2,"休假");
    @EnumValue//将注解所标识的属性的值存储在数据库中
    private Integer status;
    private String statusName;


    StatusEnum(Integer status, String statusName) {
        this.status = status;
        this.statusName = statusName;
    }
}

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

package com.example.wx_test.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.wx_test.enums.StatusEnum;
import lombok.Data;

@TableName("t_employee")
@Data
public class Employee {
    private Integer id;

    private String name;

    private String gender;

    private String email;
    private String phoneNumber;
    @TableField(value = "departmentId")
    private Integer departmentId;
    private Integer salary;
    //枚举类-员工状态
    private StatusEnum status;

}

测试类

package com.example.wx_test;

import com.example.wx_test.entity.Employee;
import com.example.wx_test.enums.StatusEnum;
import com.example.wx_test.mapper.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmployeeEnumTest {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Test
    public void test(){
        Employee employee = new Employee();
        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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值