mybatis-plus使用@EnumValue处理枚举类型的示例代码

所属分类: 软件编程 / java 阅读数: 158
收藏 0 赞 0 分享

自mybatis3.1.0开始,如果你无需使用原生枚举,可配置默认枚举来省略扫描通用枚举配置 默认枚举配置

1、配置文件配置枚举所在的包

#配置枚举 支持通配符 * 或者 ; 分割
mybatis-plus.type-enums-package=com.iscas.biz.mp.test.model.enums
mybatis-plus.configuration.default-enum-type-handler=org.apache.ibatis.type.EnumOrdinalTypeHandler

2、定义一个枚举,在需要存入数据库的字段上加上@EnumValue注解

package com.iscas.biz.mp.test.model.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonView;
import com.iscas.biz.mp.test.model.TestEntity;
import lombok.Getter;

import java.util.Objects;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/4/5 15:23
 * @since jdk1.8
 */

public enum SexEnum /*implements IEnum<Integer>*/ {

  /**
   * 男
   * */
  MAN(1, "男"),

  /**
   * 女
   * */
  WOMEN(2, "女");


  @EnumValue
  private final int code;

  @JsonValue
  public int getCode() {
    return this.code;
  }

  public String getDescription() {
    return description;
  }

  private final String description;
  SexEnum(int val, String description) {
    this.code = val;
    this.description = description;
  }

  @JsonCreator
  public static SexEnum getByCode(int code) {
    for (SexEnum value : SexEnum.values()) {
      if (Objects.equals(code, value.getCode())) {
        return value;
      }
    }
    return null;
  }
/*
  @Override
  public Integer getValue() {
    return code;
  }*/
}

3、测试实体使用枚举

package com.iscas.biz.mp.test.model;

import com.iscas.biz.mp.test.model.enums.SexEnum;
import lombok.Data;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/4/5 15:22
 * @since jdk1.8
 */
@Data
public class TestEntity {
  private String name;

  private SexEnum sex;
}

4、测试读取和存储带有枚举的实体

package com.iscas.biz.mp.test.controller;

import com.iscas.biz.mp.test.mapper.TestEntityMapper;
import com.iscas.biz.mp.test.model.enums.SexEnum;
import com.iscas.biz.mp.test.model.TestEntity;
import com.iscas.templet.common.BaseController;
import com.iscas.templet.common.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * //TODO
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2020/4/5 15:22
 * @since jdk1.8
 */
@RestController
@RequestMapping("/testEntity")
public class TestMpEnumController extends BaseController {
  @Autowired
  private TestEntityMapper testEntityMapper;

  @GetMapping("/get")
  public ResponseEntity testEntity() {
    ResponseEntity response = getResponse();
    List<TestEntity> testEntities = testEntityMapper.selectList(null);
    response.setValue(testEntities);
    return response;
  }
  @PostMapping("/post")
  public ResponseEntity testSaveEntity(@RequestBody TestEntity testEntity) {
    ResponseEntity response = getResponse();
    int insert = testEntityMapper.insert(testEntity);
    response.setValue(insert);
    return response;
  }

}

到此这篇关于mybatis-plus使用@EnumValue处理枚举类型的示例代码的文章就介绍到这了,更多相关mybatis-plus @EnumValue 枚举 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

更多精彩内容其他人还在看

Java实现一个简单的文件上传案例示例代码

这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在spring中使用JdbcTemplate操作数据库的几种方式

这篇文章主要介绍了详解在spring中使用JdbcTemplate操作数据库的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java中的switch case语句使用详解

这篇文章主要介绍了java中的switch case语句使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

深入理解Java 线程池

这篇文章主要介绍了Java 线程池的相关资料,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解mybatis #{}和${}的区别、传参、基本语法

这篇文章主要介绍了mybatis #{}和${}的区别、传参、基本语法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

这篇文章主要介绍了MyBatis 中 ${}和 #{}的正确使用方法,本文给大家提到了MyBatis 中 ${}和 #{}的区别,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Mybatis中的 ${} 和 #{}区别与用法

这篇文章主要介绍了Mybatis中的 ${} 和 #{}区别与用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Maven安装过程图文详解

这篇文章主要介绍了Maven安装过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

SpringBoot登录拦截配置详解(实测可用)

这篇文章主要介绍了SpringBoot登录拦截配置详解(实测可用),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA 内存溢出案例汇总

这篇文章主要介绍了JAVA 内存溢出案例的汇总,文中讲解非常细致,帮助各位工作学习时避免内存溢出,感兴趣的朋友可以了解下
收藏 0 赞 0 分享
查看更多