MyBatis-Plus通用枚举使用

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。MyBatis-Plus通用枚举使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

MyBatis-Plus通用枚举

平常开发中,某些实体对象涉及到状态的变化,一般使用方式有以下2种:

以员工实体举例子

1>使用常量的方式

public class Employee {
    public static final int STATUS_NORMAL  = 0;  //在职
    public static final int STATUS_LEAVE   = 1;  //离职
    //员工状态
    private int status;
    //省略其他属性
}    

在这里插入图片描述

2>使用枚举方式

//员工状态枚举
@Getter
public enum EmployeeStatus {
    NORMAL(0, "在职"), LEAVE(1,"离职");
    private int status;
    private String label;
    private EmployeeStatus(int status, String label){
        this.status = status;
        this.label = label;
    }
}
public class Employee {
    //员工状态
    private EmployeeStatus status = EmployeeStatus.NORMAL;
    //省略其他属性
}    

使用常理操作方式与枚举操作方式其实大同小异,枚举赋予状态更多的操作空间,比如对应的方法,对应的属性,灵活性更高。

MyBatis-Plus 也支持枚举字段操作

3>修改员工在职状态为例子

步骤:
1.创建EmployeeStatus 枚举类与添加status字段

在这里插入图片描述

使用SQL进行修改

在这里插入图片描述

此时会报错———

解决方案1:

实现 IEnum接口 并提供getValue()方法:
@Getter
public enum EmployeeStatus implements IEnum<Integer> {
    NORMAL(0, "在职"), LEAVE(1,"离职");
    private int status;
    private String label;
    private EmployeeStatus(int status, String label){
        this.status = status;
        this.label = label;
    }
    @Override
    public Integer getValue() {
        //在表status列中存什么值
        return this.status;
    }
}

在这里插入图片描述

方案2:

使用注解方式替换接口的实现。
@Getter
public enum EmployeeStatus{
    NORMAL(0, "在职"), LEAVE(1,"离职");
    
    @EnumValue
    private int status;
    private String label;
    private EmployeeStatus(int status, String label){
        this.status = status;
        this.label = label;
    }
}

在这里插入图片描述

在application.properties 文件配置操作类(一定要写)

明确指定枚举类所有包路径

mybatis-plus.type-enums-package=com.langfeiyes.mp.enums

步骤5:测试

@Test
public void testSave(){
    Employee employee = new Employee();
    employee.setAdmin(1);
    employee.setAge(18);
    employee.setDeptId(1L);
    employee.setEmail("zhangsan@163.com");
    employee.setName("zhangsan");
    employee.setPassword("111");
    //执行状态
    employee.setStatus(EmployeeStatus.LEAVE);
    employeeMapper.insert(employee);
}

执行SQL

==>  Preparing: INSERT INTO employee ( id, status, name, password, email, age, admin, dept_id, del, version ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
==> Parameters: 1(Long), 1(Integer), zhangsan(String), 111(String), zhangsan@163.com(String), 18(Integer), 1(Integer), 1(Long), 0(Integer), 0(Integer)

此时修改成功

查询转换:

1.表:
在这里插入图片描述
在这里插入图片描述

2.配置文件:和上面一样:

在这里插入图片描述

package cn.js.Enum;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Data;
import lombok.Getter;

@Getter
public enum SexEnum {
    MALE(1,"男"),
    FEMALE(0,"女");

    @EnumValue
    private int status;
    @JsonValue
    private String label;
    private SexEnum(int status, String label){
        this.status = status;
        this.label = label;
    }


}
package cn.js.entry;

import cn.js.Enum.SexEnum;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

/**
 * @BelongsProject: MybatisPluse-Test
 * @Author: com.js
 * @CreateTime: 2023-08-28  22:30
 * @Version: 1.0
 * @introduce:
 */

@Data
@TableName("user")
public class User {

    @TableId(value="id", type= IdType.AUTO)
    private Long id;
    private String name;
    private int  age;
    private String email;
    @TableLogic(value = "0", delval = "1")
    private Boolean del;
    private SexEnum sex;


}

2.枚举类:

package cn.js.Enum;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Data;
import lombok.Getter;

@Getter
public enum SexEnum {
    MALE(1,"男"),
    FEMALE(0,"女");

    @EnumValue
    private int status;
    @JsonValue //----------------->进行序列化操作,不加的话无法转换
    private String label;
    private SexEnum(int status, String label){
        this.status = status;
        this.label = label;
    }


}

在这里插入图片描述

5.测试


    @GetMapping("/getAll")
    public List<User>  getadd(){

        return  userService.getAll();
    }

在这里插入图片描述

在这里插入图片描述

前端传递参数用枚举接受并使用

例:插入

@PostMapping("/add")
    public String add(@RequestBody User user){
        userService.save(user);
        return "保存成功";
    }

在这里插入图片描述
在这里插入图片描述

注意:当我们贴了序列化注解后新增或者其他操作就不能传枚举名了,否则会报错

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/188528.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!