说明:mybatis plus 使用枚举可实现数据库存入时指定值保存, 读取时指定值展示(返给前端)
可通过继承IEnum<T>、 @EnumValue实现
1、引包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter-test</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
2、枚举
说明:可通过实现IEnm<T>接口的方式实现,也可通过使用@EnumValue 表签实现;读取数据时可通过使用@JsonValue 执行显示的值
import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
/**
* @author zc
* @date 2024/4/3 11:13
* @desc
*/
@AllArgsConstructor
public enum SexEnum implements IEnum<Integer> {
boy(0, "男孩"),
girl(1, "女孩");
private final Integer code;
// 序列化枚举值为 接口出参;接口入参(RequestBody),反序列化为枚举值
@JsonValue
private final String name;
@Override
public Integer getValue() {
return code;
}
}
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
/**
* @author zc
* @date 2024/4/3 11:13
* @desc
*/
@AllArgsConstructor
public enum SexEnum {
boy(0, "男孩"),
girl(1, "女孩");
@EnumValue
private final Integer code;
@JsonValue
private final String name;
}
3、bean
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.test.enums.SexEnum;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author zc
* @date 2024/3/14 17:16
* @desc
*/
@Data
@TableName("cm_host_compare_diff")
public class HostDiffBean implements Serializable {
/**
* id
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 序列号
*/
private String serialNumber;
/**
* ip
*/
private String ip;
/**
* 设备型号
*/
private String deviceNo;
/**
* 华为端ip
*/
@TableField(value = "h_ip")
private String hIp;
/**
* 华为端设备型号
*/
@TableField(value = "h_device_No")
private String hDeviceNo;
/**
* 创建时间
*/
private Date createTime;
/**
* 性别
*/
private SexEnum sex;
}
4、数据库设计
5、配置
说明:在application.yml 中配置, mybatis plus 3.5.2版本后不需要配置
mybatis-plus:
#枚举梭子包路径
type-enums-package: com.test.enums
6、测试
/**
* @author zc
* @date 2024/3/19 10:29
* @desc
*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class ApplicationTest {
@Autowired
private TestService testService;
@Test
public void test() {
try{
HostDiffBean hostDiffBean = new HostDiffBean();
hostDiffBean.setIp("127.0.0.1");
hostDiffBean.setHIp("127.0.0.2");
hostDiffBean.setCreateTime(new Date());
hostDiffBean.setSerialNumber("123");
hostDiffBean.setHDeviceNo("no123");
hostDiffBean.setDeviceNo("NO456");
hostDiffBean.setSex(SexEnum.boy);
testService.save(hostDiffBean);
}catch (Exception e){
e.printStackTrace();
}
}
通过swagger 读取