4

Mybatis 枚举类型处理器扩展之 EnumHandler

 3 years ago
source link: https://www.v2ex.com/t/797702
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Java

Mybatis 枚举类型处理器扩展之 EnumHandler

  BiChengfei · 22 小时 26 分钟前 · 460 次点击

传送门: https://github.com/bichengfei/EnumHandler

Mybatis 已经实现了很丰富的类型处理器,对于枚举类型,也提供了两种处理器:

  • EnumTypeHandler:枚举名 /枚举 Name
  • EnumOrdinalTypeHandler:枚举顺序编号,从 1 开始

但我们业务中大部分都是类似下面的枚举

public enum SexEnum {

    MAN(1, "男"),
    WOMAN(2, "女")
    ;

    public Integer key;
    public String value;

    SexEnum(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

}

我们需要在数据库中存枚举类中的字段 key,这时候官方提供的处理器就不够用了

本项目想要做的就是,在 pom.xml 中引入依赖,再在枚举类上加上 @EnumHandler,然后这个枚举类就可以使用到 Mybatis 的类型处理器的功能,方便开发


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK