1.创建通用枚举类型
package com.atguigu.mp.enums; import com.baomidou.mybatisplus.annotation.EnumValue; import lombok.Getter; @Getter public enum SexEnum { MALE(1, "男"), FEMALE(2, "女"); @EnumValue private Integer sex; private String sexName; SexEnum(Integer sex, String sexName) { this.sex = sex; this.sexName = sexName; } }
2.配置扫描通用枚举
mybatis-plus: configuration: # 配置MyBatis日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: # 配置MyBatis-Plus操作表的默认前缀 table-prefix: t_ # 配置MyBatis-Plus的主键策略 id-type: auto # 配置扫描通用枚举 type-enums-package: com.atguigu.mybatisplus.enums
3.测试
@Test public void testSexEnum(){ User user = new User(); user.setName("Enum"); user.setAge(20); //设置性别信息为枚举项,会将@EnumValue注解所标识的属性值存储到数据库 user.setSex(SexEnum.MALE); //INSERT INTO t_user ( username, age, sex ) VALUES ( ?, ?, ? ) //Parameters: Enum(String), 20(Integer), 1(Integer) userMapper.insert(user); }