Margrop
Articles158
Tags353
Categories21
1password AC AP API AppDaemon Aqara Cron Date Diagrams.net HA HADashboard HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS MySQL NAS PPPoE PostgreSQL ProcessOn Proxmox VE SSL Shell TTS TimeMachine UML Uptime Kuma Web Windows activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 光猫 公网IP 内存 内网IP 升级 启动 夏令时 天猫精灵 天翼云 安装 容器 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 抓包 描述文件 时区 显卡虚拟化 智能家居 智能音箱 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 电信 画图 直播源 续期 网关 网络风暴 群晖 腾讯 虚拟机 证书 路由 路由器 软件管家 软路由 运维监控 镜像 镜像源 门窗传感器 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

【转】Mybatis-Plus 通用枚举类型的使用

【转】Mybatis-Plus 通用枚举类型的使用

有些字段,例如性别、婚姻状况、等标志性字段,在数据库中存放的形式往往是数字,0 或者 1,这样做的好处是存取的效率高节省空间,但是前端的在展示的时候不能直接展示,需要进行一个判断,但是判断逻辑放在前端不妥,所以后端应该提前将值转换好返回该前端。

Mybatis-Plus 中我们可以使用枚举类型来完成这一操作,他能自动将数据库里的字段映射成我们需要的字段,例如性别,新建枚举类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Getter
public enum GenderType {
WOMEN(0, "女"),
MAN(1, "男");

@EnumValue
private Integer key;

@JsonValue
private String name;

GenderType(Integer key, String name) {
this.key = key;
this.name = name;
}

@Override
public String toString() {
return this.name;
}
}

其中最关键的是 @EnumValue 注解,他是标注数据库里存的字段,这里数据库里存的是 key@JsonValue 标注的是要展示的字段,这里我们想展示给前端的是 name 字段,同时要重写 toString 方法为我们想要的,因为系统会自动调用该方法作为前端的展示值,这里想要展示 name,所以直接返回它就行了。

关键点:

  • @EnumValue:标注哪一个字段是数据库里的字段;
  • @JsonValue:标注要开启自定义序列化返回值;
  • toString:具体的返回值;

同时我们需要在与数据库关联的实体类中修改类型,将性别字段改为枚举类型:

1
2
3
4
5
6
7
8
9
10
11
12
@Data
@TableName("table")
public class ZhbfDb extends SymqBaseEntity {
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private GenderType gender;
}

在配置文件中配置扫描注解类型:

#mybatis-plus 配置

1
2
mybatis-plus:
type-enums-package: com.demo.test.enums

这个时候再去查询,返回的结果就直接是我们在枚举类型中定义的 name 值了。

原文链接:https://blog.csdn.net/weixin_43941364/article/details/119821877

Author:Margrop
Link:http://blog.margrop.com/post/zhuan-mybatis-plus-tong-yong-mei-ju-lei-xing-de-shi-yong/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可