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)

相关推荐
罗政5 小时前
[附源码]超简洁个人博客网站搭建+SpringBoot+Vue前后端分离
vue.js·spring boot·后端
Java小白笔记8 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
小哇6668 小时前
Spring Boot,在应用程序启动后执行某些 SQL 语句
数据库·spring boot·sql
luoluoal11 小时前
java项目之企业级工位管理系统源码(springboot)
java·开发语言·spring boot
蜜桃小阿雯11 小时前
JAVA开源项目 校园美食分享平台 计算机毕业设计
java·jvm·spring boot·spring cloud·intellij-idea·美食
计算机学姐12 小时前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
程序员大金12 小时前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
customer0814 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Flying_Fish_roe15 小时前
Spring Boot-版本兼容性问题
java·spring boot·后端
尘浮生17 小时前
Java项目实战II基于Java+Spring Boot+MySQL的大学城水电管理系统(源码+数据库+文档)
java·开发语言·数据库·spring boot·后端·mysql·maven