-
定义枚举类
@Getter public enum SexEnum { MALE(1,"男"), FEMALE(2, "女") ; @EnumValue//表示会把这个值存入数据库 private int index; @JsonValue//json序列化时使用这个属性 private String name; SexEnum(int i, String name) { this.index = i; this.name = name; } }
-
实体类
@TableName("mytest") @Data public class MyTest { @TableId(type = IdType.AUTO) private Long id; /** * 使用mybatisPlus的EnumValue注解后就可以把这个定义为枚举类型 */ private SexEnum sex; public MyTest() { } public MyTest(Long id, SexEnum sex) { this.id = id; this.sex = sex; } }
-
测试
@Test void test2() throws JsonProcessingException { MyTest myTest = new MyTest(null, SexEnum.MALE); int insert = myTestMapper.insert(myTest); /** ==> Preparing: INSERT INTO mytest ( sex ) VALUES ( ? ) ==> Parameters: 1(Integer) */ MyTest myTest1 = myTestMapper.selectById(myTest.getId()); System.out.println(myTest1);//MyTest(id=12, sex=MALE) String string = new ObjectMapper().writeValueAsString(myTest1); System.out.println(string);//{"id":12,"sex":"男"} }