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)
此时修改成功
查询转换:
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