欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

 更新時間:2020年09月01日 14:14:38   作者:碼農(nóng)-文若書生  
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

自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)文章

  • Java中的volatile關(guān)鍵字解析

    Java中的volatile關(guān)鍵字解析

    這篇文章主要介紹了Java中的volatile關(guān)鍵字解析,Java內(nèi)存模型規(guī)定了所有的變量都存儲在主內(nèi)存中,每個線程都有自己的工作內(nèi)存,線程的工作內(nèi)存保存了該線程使用到的變量的是主內(nèi)存副本的拷貝,需要的朋友可以參考下
    2023-11-11
  • 解決線程并發(fā)redisson使用遇到的坑

    解決線程并發(fā)redisson使用遇到的坑

    這篇文章主要介紹了解決線程并發(fā)redisson使用遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • spring-boot react如何一步一步實(shí)現(xiàn)增刪改查

    spring-boot react如何一步一步實(shí)現(xiàn)增刪改查

    這篇文章主要介紹了spring-boot react如何一步一步實(shí)現(xiàn)增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 使用PageHelper插件實(shí)現(xiàn)Service層分頁

    使用PageHelper插件實(shí)現(xiàn)Service層分頁

    這篇文章主要為大家詳細(xì)介紹了使用PageHelper插件實(shí)現(xiàn)Service層分頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼

    spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼

    這篇文章主要介紹了spring security數(shù)據(jù)庫表結(jié)構(gòu)實(shí)例代碼,需要的朋友可以參考下
    2017-09-09
  • Java遍歷Map集合的方法(最新推薦)

    Java遍歷Map集合的方法(最新推薦)

    這篇文章主要介紹了Java遍歷Map集合的方法,遍歷map的key集合然后通過key獲取value,本文給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • log4j日志格式加入自定義字段信息方式

    log4j日志格式加入自定義字段信息方式

    這篇文章主要介紹了log4j日志格式加入自定義字段信息方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java并發(fā)控制機(jī)制詳解

    Java并發(fā)控制機(jī)制詳解

    這篇文章主要為大家詳細(xì)介紹了Java并發(fā)控制機(jī)制,什么是Java并發(fā)控制機(jī)制,Java并發(fā)控制機(jī)制的作用,感興趣的小伙伴們可以參考一下
    2016-08-08
  • SpringMVC的REST風(fēng)格的四種請求方式總結(jié)

    SpringMVC的REST風(fēng)格的四種請求方式總結(jié)

    下面小編就為大家?guī)硪黄猄pringMVC的REST風(fēng)格的四種請求方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java8?Stream流根據(jù)多個字段去重

    Java8?Stream流根據(jù)多個字段去重

    這篇文章主要介紹了Java8?Stream流根據(jù)多個字段去重,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論