本文使用的是 Mybatis-Plus 从 3.4.2 版本来实现通用枚举配置,不同的版本,通用枚举配置会不一样,而且从Mybatis-Plus 从 3.5.2 版本开始只需使用 @EnumValue 注解枚举属性,就可以实现一系列功能,而不需要其他配置。
这里以性别为例,性别枚举:男用(1)表示,女用(2)表示
添加依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
声明通用枚举属性
方式一: 使用 @EnumValue 注解枚举属性
package com.example.student.enmus;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author CuMamba
* @date 2024/3/18
*/
@AllArgsConstructor
@Getter
public enum SexEnum{
MALE(1, "男"),
FEMALE(2, "女");
@EnumValue //标记数据库存的值是code
private final Integer code;
@JsonValue //标记响应json值
private final String name;
}
方式二: 枚举属性,实现 IEnum
package com.example.student.enmus;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author CuMamba
* @date 2024/3/18
*/
@AllArgsConstructor
@Getter
public enum SexEnum implements IEnum<Integer> {
MALE(1, "男"),
FEMALE(2, "女");
private final Integer code;
@JsonValue //标记响应json值
private final String name;
@Override
public Integer getValue() {
return this.code;
}
}
实体属性使用枚举类型
package com.example.student.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import com.example.student.enmus.SexEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
* 访客登记表
* </p>
*
* @author CuMamba
* @since 2024-03-18
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("visitor")
@ApiModel(value="Visitor对象", description="访客登记表")
public class Visitor implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "性别")
private SexEnum sex;
@ApiModelProperty(value = "电话")
private String phone;
@ApiModelProperty(value = "访客人数")
private Integer visitorCount;
@ApiModelProperty(value = "来访时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date visitTime;
@ApiModelProperty(value = "事情")
private String content;
@ApiModelProperty(value = "创建人")
@TableField(fill = FieldFill.INSERT)
private Integer creatorId;
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@ApiModelProperty(value = "更新人")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Integer updaterId;
@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
配置扫描通用枚举
注意!! 从 3.5.2 开始无需配置
mybatis-plus:
# 扫描通用枚举 从 3.5.2 开始无需配置
type-enums-package: com.example.student.enmus
数据库表结构
create table student.visitor
(
id int auto_increment
primary key,
name varchar(64) not null comment '姓名',
sex tinyint not null comment '性别',
phone varchar(11) not null comment '电话',
visitor_count int not null comment '访客人数',
visit_time datetime default CURRENT_TIMESTAMP not null comment '来访时间',
content varchar(255) not null comment '事情',
creator_id int unsigned not null comment '创建人',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater_id int unsigned not null comment '更新人',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间'
)
comment '访客登记表' charset = utf8;
添加controller校验
添加新增和查询接口
@RestController
@Api(tags = "访客登记表管理")
public class VisitorController {
@Resource
private VisitorService visitorService;
@ApiOperation("访客登记表-添加")
@PostMapping("/visitor")
public R add(@RequestBody Visitor visitor){
visitorService.save(visitor);
return R.success();
}
@ApiOperation("访客登记表-详情")
@GetMapping("/visitor/{id}" )
public R detail(@PathVariable Integer id){
Visitor detail = visitorService.getById(id);
return R.success(detail);
}
}