注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
本章节将介绍 @EnumValue 注解的用法,该注解用来将数据库定义的枚举类型和Java的枚举进行映射。注意、该注解需要添加到枚举类的某个字段上面,而不是添加到 JavaBean 实体的某个字段上面。
假如我们在数据库 user 表中有一个 week 字段,表示周信息。在项目中存在一个 WeekEnum 枚举类,可以在该枚举类中使用 @EnumValue 注解,代码如下:
public enum WeekEnum {
SUNDAY("Sunday", "星期日"),
SATURDAY("Saturday", "星期六"),
FRIDAY("Friday", "星期五"),
THURSDAY("Thursday", "星期四"),
WEDNESDAY("Wednesday", "星期三"),
TUESDAY("Tuesday", "星期二"),
MONDAY("Monday", "星期一");
@EnumValue
private String value;
private String name;
// ...
}上面代码将 @EnumValue 注解标注在 value 字段,表示该字段的值和数据库表中定义的枚举值一一对应。在 JavaBean 中就可以直接引用该枚举了,如下:
public class User {
private int id;
private String name;
private String sex;
private WeekEnum week;
}(1)向数据库 user 表中添加一个枚举类型的 week 字段,sql 语句如下:
-- 添加一个枚举类型的列,week
ALTER TABLE `user`
ADD COLUMN `week` enum('Sunday','Saturday','Friday','Thursday','Wednesday','Tuesday','Monday') NULL DEFAULT '';(2)创建要给 WeekEnum 枚举类,该类中的 value 字段使用 @EnumValue 字段进行修饰,代码如下:
package com.hxstrive.mybatis_plus.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
public enum WeekEnum {
SUNDAY("Sunday", "星期日"),
SATURDAY("Saturday", "星期六"),
FRIDAY("Friday", "星期五"),
THURSDAY("Thursday", "星期四"),
WEDNESDAY("Wednesday", "星期三"),
TUESDAY("Tuesday", "星期二"),
MONDAY("Monday", "星期一");
@EnumValue
private String value;
private String name;
private WeekEnum(String value, String name) {
this.value = value;
this.name = name;
}
public String getValue() {
return this.value;
}
public String getName() {
return this.name;
}
}(3)定义一个 User 类,其中 week 成员变量的类型为 WeekEnum。代码如下:
package com.hxstrive.mybatis_plus.model;
import com.baomidou.mybatisplus.annotation.*;
import com.hxstrive.mybatis_plus.enums.WeekEnum;
@TableName(value = "user")
public class AnnotationUser6Bean {
@TableId(value = "user_id", type = IdType.AUTO)
private int userId;
@TableField("name")
private String name;
@TableField("sex")
private String sex;
@TableField("age")
private Integer age;
@TableField("week")
private WeekEnum week;
// 忽略 getter 和 setter
@Override
public String toString() {
String weekStr = "IS NULL";
if(this.week != null) {
weekStr = "[" + this.week.getValue() + "] " + this.week.getName();
}
return "UserBean{" +
"userId=" + userId +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", week=" + weekStr +
'}';
}
}(4)配置 MyBatis Plus 自动扫描我们定义的枚举类型,配置如下:
# mybatis plus mybatis-plus: # 支持统配符 * 或者 ; 分割 typeEnumsPackage: com.hxstrive.mybatis_plus.enums
如果你忘记了上面的配置,运行程序会抛出“No enum constant com.hxstrive.mybatis_plus.enums.WeekEnum.Friday”错误信息。
(5)客户端代码:
package com.hxstrive.mybatis_plus.simple_mapper.annotation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.AnnotationUser6Mapper;
import com.hxstrive.mybatis_plus.model.AnnotationUser6Bean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotationDemo6 {
@Autowired
private AnnotationUser6Mapper userMapper;
@Test
void contextLoads() throws Exception {
QueryWrapper<AnnotationUser6Bean> wrapper = new QueryWrapper<>();
wrapper.lt("user_id", 10);
for(AnnotationUser6Bean item : userMapper.selectList(wrapper)) {
System.out.println(item);
}
}
}