首页 > 编程语言 > mybatis-plus使用@EnumValue处理枚举类型的示例代码
2020
10-10

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

自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 枚举 内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧