mybatis-plus使用@EnumValue處理枚舉類型的示例代碼
自mybatis3.1.0開始,如果你無需使用原生枚舉,可配置默認(rèn)枚舉來省略掃描通用枚舉配置 默認(rèn)枚舉配置
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、定義一個枚舉,在需要存入數(shù)據(jù)庫的字段上加上@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、測試實(shí)體使用枚舉
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、測試讀取和存儲帶有枚舉的實(shí)體
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; } }
到此這篇關(guān)于mybatis-plus使用@EnumValue處理枚舉類型的示例代碼的文章就介紹到這了,更多相關(guān)mybatis-plus @EnumValue 枚舉 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring-boot react如何一步一步實(shí)現(xiàn)增刪改查
這篇文章主要介紹了spring-boot react如何一步一步實(shí)現(xiàn)增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11使用PageHelper插件實(shí)現(xiàn)Service層分頁
這篇文章主要為大家詳細(xì)介紹了使用PageHelper插件實(shí)現(xiàn)Service層分頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼
這篇文章主要介紹了spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼,需要的朋友可以參考下2017-09-09SpringMVC的REST風(fēng)格的四種請求方式總結(jié)
下面小編就為大家?guī)硪黄猄pringMVC的REST風(fēng)格的四種請求方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08