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

时间:2021-09-04 18:11:04

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

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

?
1
2
3
#配置枚举 支持通配符 * 或者 ; 分割
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注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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、测试实体使用枚举

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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、测试读取和存储带有枚举的实体

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 枚举 内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u011943534/article/details/105543879