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)

相关推荐
小编码上说1 小时前
LSH(局部敏感哈希)分桶,海量数据下的相似性搜索解决方案
java·spring boot·缓存·langchain4j·lsh·局部敏感哈希·ai调用优化
计算机_毕业设计1 小时前
java-springboot数字藏品系统 基于 SpringBoot 的区块链数字艺术品交易平台 Java 微服务架构下的加密藏品展示与拍卖系统计算机毕业设计
java·spring boot·课程设计
dovens2 小时前
SpringBoot集成MQTT客户端
java·spring boot·后端
❀͜͡傀儡师2 小时前
Spring Boot 集成 RocksDB 实战:打造高性能 KV 存储加速层
java·spring boot·后端·rocksdb
NaMM CHIN3 小时前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring
ATCH IERV3 小时前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
钰衡大师5 小时前
Activiti 7 工作流技术文档
java·数据库·spring boot
Ruci ALYS6 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
rADu REME6 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
你好潘先生6 小时前
Next.js + Spring Boot 实现 AI 多模型并行对话系统(架构设计与关键实现)
spring boot·向量检索·next.js·pgvector·ai对话·多模型对比·sse流式输出