自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