MyBatisPlus(十七)通用枚举

说明

MyBatisPlus 优雅地使用枚举类型。

声明通用枚举属性

使用 @EnumValue 注解枚举属性

java 复制代码
package com.example.web.enumeration;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;

/**
 * 性别枚举
 */
@AllArgsConstructor
public enum GenderEnum {

    UNKNOWN(0, "未知"),
    MALE(1, "男"),
    FEMALE(2, "女");

    @EnumValue // 标记数据库存的值是 code
    @JsonValue // 序列化枚举值为 接口出参;接口入参(RequestBody),反序列化为枚举值
    private final int code;

    private final String description;

}

实体属性使用枚举类型

java 复制代码
package com.example.web.entity;

import com.example.web.enumeration.GenderEnum;
import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    private Integer deleted;
    private GenderEnum gender;
}

数据库关系模式

测试

新增测试

代码

java 复制代码
    /**
     * 插入用户(男性)
     */
    @Test
    public void insertMale() {
        User user = new User();
        user.setId(10L);
        user.setName("钱一");
        user.setAge(26);
        user.setEmail("qianyi@example.com");
        user.setGender(GenderEnum.MALE);

        mapper.insert(user);
    }


    /**
     * 插入用户(女性)
     */
    @Test
    public void insertFemale() {
        User user = new User();
        user.setId(11L);
        user.setName("钱二");
        user.setAge(26);
        user.setEmail("qianer@example.com");
        user.setGender(GenderEnum.FEMALE);

        mapper.insert(user);
    }


    /**
     * 插入用户(未填写性别)
     */
    @Test
    public void insertUnknown() {
        User user = new User();
        user.setId(12L);
        user.setName("钱三");
        user.setAge(26);
        user.setEmail("qiansan@example.com");

        mapper.insert(user);
    }

结果

插入用户(男性):

插入用户(女性):

插入用户(未填写性别):

数据库中的数据:

查询测试

java 复制代码
    @Test
    public void selectById() {
        User user = mapper.selectById(10);
        log.info("user:{}", user);
    }

序列化枚举值为接口参数值

  1. 序列化枚举值为接口出参
  2. 接口入参(RequestBody),反序列化为枚举值。

这两种实现,都是通过 @JsonValue 注解实现的。

测试:序列化枚举值为接口出参

java 复制代码
    @GetMapping
    public List<User> selectAll() {
        return userService.list();
    }

测试:接口入参(RequestBody),反序列化为枚举值

java 复制代码
    @PostMapping
    public void addUser(@Valid @RequestBody User param) {
        log.info("新增用户:param={}", param);
    }

服务器接收到的数据,打印log

新增用户:param=User(id=70, name=小明, age=20, email=xiaoming@qq.com, deleted=0, gender=MALE)

Query参数,使用枚举(待完善,暂不推荐)

当前方法,使用枚举的名字作为请求参数。

更好的办法是,使用枚举值,和Body参数以及数据库中定义保持一致。

配置代码

java 复制代码
package com.example.core.config;

import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // 通过ApplicationConversionService向应用中注入Converter
        ApplicationConversionService.configure(registry);
    }
}

接口测试

java 复制代码
    @GetMapping("selectByUser")
    public List<User> selectByUser(User query) {
        log.info("查询用户:query={}", query);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(query.getGender()), User::getGender, query.getGender());
        return userService.list(wrapper);
    }

Log日志:

查询用户:query=User(id=null, name=null, age=null, email=null, deleted=null, gender=MALE)

相关推荐
无人不xiao2 小时前
springBoot 实现 接口进度条
java·spring boot·后端
smileNicky2 小时前
Docker 部署 SpringBoot 项目超详细教程
spring boot·docker·容器
HLAIA光子3 小时前
这些Spring Boot写法已经过时了!
spring boot·后端
i220818 Faiz Ul4 小时前
宠物猫之猫咖管理系统|基于java + vue宠物猫之猫咖管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·宠物猫之猫咖管理系统
i220818 Faiz Ul4 小时前
二手交易系统|基于springboot + vue二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·二手交易系统
逍遥德4 小时前
SpringBoot自带TaskScheduler 接口实现定时任务的动态增、删、启、停。
java·spring boot·后端·中间件
奋斗的小乌龟7 小时前
langchain4j笔记-08
java·spring boot·笔记
小英雄大肚腩丶7 小时前
RabbitMQ消息队列
java·数据结构·spring boot·分布式·rabbitmq·java-rabbitmq
一 乐9 小时前
学院教学工作量统计|基于java+ vue学院教学工作量统计管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·学院教学工作量统计系统
静小谢10 小时前
sql笔记
spring boot·笔记·sql·mybatis