枚举类
使用 @EnumValue注解标识数据库字段
package com.example.enumpackage;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* @description: mybatis-plus通用枚举 ||性别
* @author: GuoTong
* @createTime: 2021-08-21 20:38
* @since JDK 1.8 OR 11
**/
public enum GenderTypeEnum {
WOMAN(0, "女"),
MAN(1, "男"),
SECURITY(2, "保密");
//数据库存入的性别字段:0标识女 ,1标识男,2标识保密
@EnumValue
@JsonValue //标记响应json值
private Integer gender;
private String chinaMsg;
GenderTypeEnum(int gender, String chinaMsg) {
this.gender = gender;
this.chinaMsg = chinaMsg;
}
@Override
public String toString() {
return "GenderTypeEnum{" +
"gender=" + gender +
", chinaMsg='" + chinaMsg + '\'' +
'}';
}
}
建立枚举解析策略
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer(){
return builder -> builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
}
回头再枚举数据库字段加上解析注解: @JsonValue //标记响应json值
测试
@Test
public void TestCommonEnum() {
//通用枚举|将性别转化为1男,0女去查询
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("gender", GenderTypeEnum.WOMAN);
List<User> users = service.list(wrapper);
System.out.println(users);
}
结果:
[{"id":5,"name":"蚂蚁金服","gender":"0","age":23,"address":"月牙湾","qq":"23435546","email":"gsgrv@co.com"},{"id":7,"name":"瑞雯","gender":"0","age":23,"address":"召唤师峡谷","qq":"9090960","email":"ruiwen@13.com"},{"id":8,"name":"汤姆","gender":"0","age":23,"address":"上海市","qq":"8967340","email":"tom@google.com"},{"id":9,"name":"刘德华","gender":"0","age":233,"address":"重庆市","qq":"23464575","email":"wangfei@qq.cn"}]