Spring boot中实现字典管理

数据库脚本

CREATE TABLE `data_dict` (
  `id` bigint NOT NULL COMMENT '主键',
  `dict_code` varchar(32) DEFAULT NULL COMMENT '字典编码',
  `dict_name` varchar(64) DEFAULT NULL COMMENT '字典名称',
  `dict_description` varchar(255) DEFAULT NULL COMMENT '字典描述',
  `dict_status` tinyint DEFAULT NULL COMMENT '字典状态;0:禁用;1:启用',
  `created_by` varchar(32) DEFAULT NULL COMMENT '创建人;姓名[域账号]',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_by` varchar(32) DEFAULT NULL COMMENT '更新人;姓名[域账号]',
  `updated_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='数据字典';

INSERT INTO data_dict (id, dict_code, dict_name, dict_description, dict_status, created_by, created_time, updated_by, updated_time) VALUES(1889143437849174016, 'test', 'test', 'test', 1, 'xx', '2025-02-11 10:44:29', 'xx', '2025-02-11 10:44:29');
INSERT INTO data_dict (id, dict_code, dict_name, dict_description, dict_status, created_by, created_time, updated_by, updated_time) VALUES(1889143437849174017, 'test2', 'test2', 'test2', 1, 'xx', '2025-02-11 10:44:29', 'xx', '2025-02-11 10:44:29');



CREATE TABLE `data_dict_item` (
  `id` bigint NOT NULL COMMENT '主键',
  `dict_code` varchar(32) DEFAULT NULL COMMENT '字典编码',
  `item_code` varchar(32) DEFAULT NULL COMMENT '条目编码',
  `item_value` varchar(255) DEFAULT NULL COMMENT '条目值',
  `item_description` varchar(255) DEFAULT NULL COMMENT '条目描述',
  `item_status` tinyint DEFAULT NULL COMMENT '条目状态;0:禁用;1:启用',
  `seq` int DEFAULT NULL COMMENT '序号',
  `created_by` varchar(32) DEFAULT NULL COMMENT '创建人;姓名[域账号]',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_by` varchar(32) DEFAULT NULL COMMENT '更新人;姓名[域账号]',
  `updated_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='数据字典条目';

INSERT INTO data_dict_item (id, dict_code, item_code, item_value, item_description, item_status, seq, created_by, created_time, updated_by, updated_time) VALUES(1889143437849174016, 'test', 'xxx', 'xx', 'xxx', 1, 1, 'xx', '2025-02-11 10:44:29', 'xx', '2025-02-11 10:44:29');
INSERT INTO data_dict_item (id, dict_code, item_code, item_value, item_description, item_status, seq, created_by, created_time, updated_by, updated_time) VALUES(1889143437849174017, 'test2', '111', '222', 'xxx', 1, 1, 'xx', '2025-02-11 10:44:29', 'xx', '2025-02-11 10:44:29');
INSERT INTO data_dict_item (id, dict_code, item_code, item_value, item_description, item_status, seq, created_by, created_time, updated_by, updated_time) VALUES(1889143437849174018, 'test2', '33', '44', 'xxx', 1, 1, 'xx', '2025-02-11 10:44:29', 'xx', '2025-02-11 10:44:29');

代码

package com.demo.dto.sys;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * 字典缓存
 *
 * @since 2025/02/11 上午 10:39
 */
@Data
@Accessors(chain = true)
public class DataDictItemCache implements Serializable {

    @Serial
    private static final long serialVersionUID = -2394718872868472043L;

    /**
     * 字典编码
     */
    private String dictCode;
    /**
     * 条目编码
     */
    private String itemCode;
    /**
     * 条目值
     */
    private String itemValue;
    /**
     * 条目状态;0:禁用;1:启用
     */
    private Integer itemStatus;
}

package com.demo.annotation;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.demo.config.DictSerializer;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 数据字典注解
 *
 * @since 2025/02/08 下午 01:41
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = DictSerializer.class)
public @interface Dict {

    /**
     * 字典编码
     */
    String dictCode();
}

// 使用方式如下

// @Data
// public class Test {
//
//     @Dict(dictCode = "xxx")
//     private String itemCode;
//
//     private String other;
// }

// {
//     "code": 200,
//     "message": "操作成功",
//     "result": {
//         "itemCode": {
//             "dictCode": "xxx",
//             "itemCode": "111",
//             "itemValue": "222",
//             "itemStatus": 1
//         },
//         "other": null
//     },
//     "timestamp": "1739245075643"
// }
package com.demo.config;

import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.demo.annotation.Dict;
import org.springframework.stereotype.Component;

import java.io.Serial;

/**
 * 字典注解序列化拦截器
 *
 * @since 2025/02/08 下午 02:20
 */
@Component
public class DictAnnotationIntrospector extends NopAnnotationIntrospector {

    @Serial
    private static final long serialVersionUID = 5139608634773791712L;

    @Override
    public Object findSerializer(Annotated am) {
        Dict dict = am.getAnnotation(Dict.class);
        if (dict != null) {
            return DictSerializer.class;
        }
        return null;
    }
}
package com.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 修改被@Dict注解标注的属性字典序列化方式
 *
 * @since 2025/02/08 下午 02:22
 */
@Configuration
public class DictSerializerConfig {

    @Autowired
    private DictAnnotationIntrospector dictAnnotationIntrospector;

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.annotationIntrospector(dictAnnotationIntrospector);
    }
}
package com.demo.config;

import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.demo.annotation.Dict;
import com.demo.service.sys.DataDictItemService;
import com.demo.dto.sys.DataDictItemCache;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.io.Serial;
import java.util.List;

/**
 * 数据字典自定序列化类
 *
 * @since 2025/02/08 下午 02:18
 */
public class DictSerializer extends StdSerializer<Object> implements ContextualSerializer {

    @Serial
    private static final long serialVersionUID = -3774620761859983477L;

    /**
     * 字典编码
     */
    private final String dictCode;

    public DictSerializer() {
        super(Object.class);
        this.dictCode = null;
    }

    public DictSerializer(String dictCode) {
        super(Object.class);
        this.dictCode = dictCode;
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {
        Dict annotation = property.getAnnotation(Dict.class);
        if (annotation != null) {
            return new DictSerializer(annotation.dictCode());
        }
        return this;
    }

    @Override
    public void serialize(Object itemCode, JsonGenerator gen, SerializerProvider provider) throws IOException {
        DataDictItemCache itemCache = null;

        if (StringUtils.isNotBlank(dictCode) && ObjectUtils.isNotEmpty(itemCode)) {
            DataDictItemService service = SpringUtil.getBean(DataDictItemService.class);
            List<DataDictItemCache> allItemCache = service.getDictItemCacheByDictCode(dictCode);
            if (!CollectionUtils.isEmpty(allItemCache)) {
                itemCache = allItemCache.stream()
                        .filter(item -> item.getItemCode().equals(itemCode))
                        .findFirst()
                        .orElse(null);
            }
        }

        gen.writeObject(itemCache);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/968790.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

uniapp 使用 鸿蒙开源字体

uniapp vue3 使用 鸿蒙开源字体 我的需求是全局使用鸿蒙字体。 所以&#xff1a; 0. 首先下载鸿蒙字体&#xff1a; 鸿蒙资源 下载后解压&#xff0c;发现里面有几个文件夹&#xff1a; 字体名称说明Sans默认的鸿蒙字体&#xff0c;支持基本的多语言字符&#xff08;包括字…

【ESP32指向鼠标】——icm20948与esp32通信

【ESP32指向鼠标】——icm20948与esp32通信 ICM-20948介绍 ICM-20948 是一款由 InvenSense&#xff08;现为 TDK 的一部分&#xff09;生产的 9 轴传感器集成电路。它结合了 陀螺仪、加速度计和磁力计。 内置了 DMP&#xff08;Digital Motion Processor&#xff09;即负责执…

机器学习(李宏毅)——BERT

一、前言 本文章作为学习2023年《李宏毅机器学习课程》的笔记&#xff0c;感谢台湾大学李宏毅教授的课程&#xff0c;respect&#xff01;&#xff01;&#xff01; 读这篇文章必须先了解self-attention、Transformer&#xff0c;可参阅我其他文章。 二、大纲 BERT简介self-…

NO.12十六届蓝桥杯备战|关系操作符|操作符连用|浮点数比较|练习2道(C++)

关系操作符 关系操作符介绍 ⽤于⽐较的表达式&#xff0c;称为“关系表达式”&#xff08;relational expression&#xff09;&#xff0c;⾥⾯使⽤的运算符就称为“关 系运算符”&#xff08;relational operator&#xff09;&#xff0c;主要有下⾯6个。 运算符描述>⼤…

JVM组成

JVM是什么&#xff1f; JVM&#xff08;Java Virtual Machine&#xff09;&#xff1a;Java程序的运行环境(java二进制字节码的运行环境) 好处&#xff1a; 1.一次编写&#xff0c;到处运行 Java代码是如何做到一次编写&#xff0c;到处运行&#xff1f; 计算机的最底层是计…

不小心删除服务[null]后,git bash出现错误

不小心删除服务[null]后&#xff0c;git bash出现错误&#xff0c;如何解决&#xff1f; 错误描述&#xff1a;打开 git bash、msys2都会出现错误「bash: /dev/null: No such device or address」 问题定位&#xff1a; 1.使用搜索引擎搜索「bash: /dev/null: No such device o…

130,[1] 攻防世界 very_easy_sql

进入靶场 典型SQL注入页面 先查看源码 访问 试试http://127.0.0.1/ 还尝试了其他都是nonono 回归第一个登录页面 提交的内容不在url处显示&#xff0c;反而第二个url页面会在url处显示 明白第一个页面是通过post方式提交&#xff0c;反正没得到什么信息&#xff0c;去抓…

Android10 音频参数导出合并

A10 设备录音时底噪过大&#xff0c;让音频同事校准了下&#xff0c;然后把校准好的参数需要导出来&#xff0c;集成到项目中&#xff0c;然后出包&#xff0c;导出方式在此记录 设备安装debug系统版本调试好后&#xff0c; adb root adb remount adb shell 进入设备目录 导…

django中间件,中间件给下面传值

1、新建middleware.py文件 # myapp/middleware.py import time from django.http import HttpRequest import json from django.http import JsonResponse import urllib.parse from django.core.cache import cache from comm.Db import Db class RequestTimeMiddleware:def …

24电子信息类研究生复试面试问题汇总 电子信息类专业知识问题最全!电子信息复试全流程攻略 电子信息考研复试真题汇总

你是不是在为电子信息考研复试焦虑&#xff1f;害怕被老师问到刁钻问题、担心专业面答不上来&#xff1f;别慌&#xff01;作为复试面试92分逆袭上岸的学姐&#xff0c;今天手把手教你拆解电子信息类复试通关密码&#xff01;看完这篇&#xff0c;让你面试现场直接开大&#xf…

LVDS接口总结--(1)LVDS硬件电路接口

1.LVDS差分信号电路原理 LVDS指的是低压差分信号&#xff0c;是一种电平标准。 差分信号在串行通信中有着非常广泛的应用&#xff0c;典型应用有PCIE中的gen1&#xff0c;gen2&#xff0c;gen3&#xff0c;gen4&#xff0c;gen5&#xff0c;SATA接口&#xff0c;USB接口等。 …

【STM32】HAL库Host MSC读写外部U盘及FatFS文件系统的USB Disk模式

【STM32】HAL库Host MSC读写外部U盘及FatFS文件系统的USB Disk模式 在先前 分别介绍了FatFS文件系统和USB虚拟U盘MSC配置 前者通过MCU读写Flash建立文件系统 后者通过MSC连接电脑使其能够被操作 这两者可以合起来 就能够实现同时在MCU、USB中操作Flash的文件系统 【STM32】通过…

第四期书生大模型实战营-第5关-L2G5000

1 Web 版茴香豆 助教说这趴先跳过&#xff0c;那我就跳过 2 茴香豆本地标准版搭建 2.1 环境搭建 2.2 安装茴香豆 2.3 知识库创建 2.4 测试知识助手 2.4.1 命令行运行 2.4.2 Gradio UI 界面测试 确认一下是否正常 完美。 至于选做&#xff0c;我这么懒&#xff0c;怎么可能…

Elastic Cloud Serverless 现已在 Microsoft Azure 上提供技术预览版

作者&#xff1a;来自 Elastic Yuvi Gupta Elastic Cloud Serverless 提供了启动和扩展安全性、可观察性和搜索解决方案的最快方法 — 无需管理基础设施。 今天&#xff0c;我们很高兴地宣布 Microsoft Azure 上的 Elastic Cloud Serverless 技术预览版现已在美国东部地区推出。…

go语言简单快速的按顺序遍历kv结构(map)

文章目录 需求描述用map实现按照map的key排序用二维切片实现用结构体实现 需求描述 在go语言中&#xff0c;如果需要对map遍历&#xff0c;每次输出的顺序是不固定的&#xff0c;可以考虑存储为二维切片或结构体。 假设现在需要在页面的下拉菜单中展示一些基础的选项&#xff…

TDengine 产品由哪些组件构成

目 录 背景产品生态taosdtaosctaosAdaptertaosKeepertaosExplorertaosXtaosX Agent应用程序或第三方工具 背景 了解一个产品&#xff0c;最好从了解产品包括哪些内容开始&#xff0c;我这里整理了一份儿 TDegnine 产品包括有哪些组件&#xff0c;每个组件作用是什么的说明&a…

2.buuctf [NPUCTF2020]ReadlezPHP(类与对象、类的属性、序列化、代码复用与封装)

进入题目页面如下 哎呦&#xff0c;有趣哈 ctrlu查看源码&#xff0c;下拉看到 点进去看看 看到源码 开始审代码 <?php // #error_reporting(0); 这行代码被注释掉了&#xff0c;原本的作用是关闭所有PHP错误报告 // 定义一个名为 HelloPhp 的类 class HelloPhp {// 声明…

Spring MVC 拦截器(Interceptor)与过滤器(Filter)的区别?

1、两者概述 拦截器&#xff08;Interceptor&#xff09;&#xff1a; 只会拦截那些被 Controller 或 RestController 标注的类中的方法处理的请求&#xff0c;也就是那些由 Spring MVC 调度的请求。过滤器&#xff08;Filter&#xff09;&#xff1a; 会拦截所有类型的 HTTP …

多机器人系统的大语言模型:综述

25年2月来自 Drexel 大学的论文“Large Language Models for Multi-Robot Systems: A Survey”。 大语言模型 (LLM) 的快速发展为多机器人系统 (MRS) 开辟新的可能性&#xff0c;从而增强通信、任务规划和人机交互。与传统的单机器人和多智体系统不同&#xff0c;MRS 带来独特…

搭建Spark集群(CentOS Stream 9)

零、资源准备 虚拟机相关: VMware workstation 16:虚拟机/vmware_16.zip(建议选择vmware_17版本)CentOS Stream 9:虚拟机/CentOS-Stream-9-latest-x86_64-boot.iso(安装包小,安装时需要联网下载)/ 虚拟机/CentOS-Stream-9-latest-x86_64-dvd1.iso(安装包大)JDK jdk1.8:…