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

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

 更新時(shí)間:2020年09月01日 14:14:38   作者:碼農(nóng)-文若書生  
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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、定義一個(gè)枚舉,在需要存入數(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、測試讀取和存儲(chǔ)帶有枚舉的實(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實(shí)現(xiàn)獲取cpu、內(nèi)存、硬盤、網(wǎng)絡(luò)等信息的方法示例

    Java實(shí)現(xiàn)獲取cpu、內(nèi)存、硬盤、網(wǎng)絡(luò)等信息的方法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)獲取cpu、內(nèi)存、硬盤、網(wǎng)絡(luò)等信息的方法,涉及java使用第三方j(luò)ar包針對本機(jī)硬件的cpu、內(nèi)存、硬盤、網(wǎng)絡(luò)信息等的讀取相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • SpringBoot項(xiàng)目如何設(shè)置權(quán)限攔截器和過濾器

    SpringBoot項(xiàng)目如何設(shè)置權(quán)限攔截器和過濾器

    這篇文章主要介紹了使用lombok時(shí)如何自定義get、set方法問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java微服務(wù)詳解及完整代碼示例

    Java微服務(wù)詳解及完整代碼示例

    微服務(wù)是一種架構(gòu)風(fēng)格,它要求我們在開發(fā)一個(gè)應(yīng)用的時(shí)候,這個(gè)應(yīng)用必須構(gòu)建成一系列小服務(wù)的組合,這篇文章主要介紹了Java微服務(wù)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-07-07
  • SpringBoot整合junit與Mybatis流程詳解

    SpringBoot整合junit與Mybatis流程詳解

    這篇文章主要介紹了SpringBoot整合第三方技術(shù),包括整合Junit、整合Mybatis,本文通過實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • java暴力匹配及KMP算法解決字符串匹配問題示例詳解

    java暴力匹配及KMP算法解決字符串匹配問題示例詳解

    這篇文章主要為大家介紹了java算法中暴力匹配算法及KMP算法解決字符串匹配的問題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • iReport使用教程(示例教程)

    iReport使用教程(示例教程)

    在使用ireport的過程中,因?yàn)楦鞣N功能都要百度,但是大家使用的例子又千差萬別讓人很苦惱,所以用一個(gè)簡單例子貫穿的展示一下ireport的常見功能
    2021-10-10
  • java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時(shí)在這里做一些分享,主要目的是讓初學(xué)者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • JAVA多線程知識匯總

    JAVA多線程知識匯總

    這篇文章主要介紹了JAVA多線程的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • vue數(shù)據(jù)響應(yīng)式原理重寫函數(shù)實(shí)現(xiàn)數(shù)組響應(yīng)式監(jiān)聽

    vue數(shù)據(jù)響應(yīng)式原理重寫函數(shù)實(shí)現(xiàn)數(shù)組響應(yīng)式監(jiān)聽

    Vue的通過數(shù)據(jù)劫持的方式實(shí)現(xiàn)數(shù)據(jù)的雙向綁定,即使用Object.defineProperty()來實(shí)現(xiàn)對屬性的劫持,但是Object.defineProperty()中的setter是無法直接實(shí)現(xiàn)數(shù)組中值的改變的劫持行為的,需要的朋友可以參考下
    2023-05-05
  • maven配置多個(gè)倉庫的實(shí)現(xiàn)

    maven配置多個(gè)倉庫的實(shí)現(xiàn)

    本文主要介紹了maven配置多個(gè)倉庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評論