我们在日常的项目开发中,对于数据字典肯定不模糊,它帮助了我们更加方便快捷地进行开发,下面一起来看看在 SpringBoot 中如何实现数据字典功能的

一、简介

1、定义

 数据字典是指对数据的数据项、数据结构、数据流、数据存储、处理逻辑等进行定义和描述,其目的是对数据流程图中的各个元素做出详细的说明,使用数据字典为简单的建模项目。简而言之,数据字典是描述数据的信息集合,是对系统中使用的所有数据元素的定义的集合。数据字典(Data dictionary)是一种用户可以访问的记录数据库和应用程序元数据的目录。主动数据字典是指在对数据库或应用程序结构进行修改时,其内容可以由DBMS自动更新的数据字典。被动数据字典是指修改时必须手工更新其内容的数据字典。

2、理解

数据字典是一种通用的程序设计思想,将主体与分支存于两张数据表中,他们之间靠着唯一的 code 相互联系,且 code 是唯一存在的,分支依附主体而存在,每一条分支都有它唯一对应的属性值

例如:性别(sex),分为(0–保密1–男2–女),那么数据字典的设计就应该是

主表:

{"code": "sex","name": "性别"
}

副表:

[{"dictCode": "sex","code": "0","text": "保密"},{"dictCode": "sex","code": "1","text": "男"},{"dictCode": "sex","code": "2","text": "女"}
]

那么我们在使用数据字典的时候,只需要知道 dictCode,再使用 code 找到唯一的字典值

二、数据表设计

1、数据表设计

主表:

drop table if exists sys_dict;/*==============================================================*/
/* Table: sys_dict                                              */
/*==============================================================*/
create table sys_dict
(id                   bigint(20) not null auto_increment comment '主键id',code                 varchar(32) comment '编码',name                 varchar(32) comment '名称',descript             varchar(64) comment '描述',status               tinyint(1) default 0 comment '状态(0--正常1--冻结)',create_time          datetime comment '创建时间',create_user          bigint(20) comment '创建人',del_flag             tinyint(1) default 0 comment '删除状态(0,正常,1已删除)',primary key (id)
)
type = InnoDB;alter table sys_dict comment '字典管理表';

副表:

drop table if exists sys_dict_detail;/*==============================================================*/
/* Table: sys_dict_detail                                       */
/*==============================================================*/
create table sys_dict_detail
(id                   bigint(20) not null comment '主键id',dict_code            varchar(32) comment '字典编码',code                 varchar(32) comment '编码',name                 varchar(32) comment '名称',primary key (id)
)
type = InnoDB;alter table sys_dict_detail comment '字典配置表';

它们的关系如图所示:

2、数据字典配置

三、开发前戏

1、引入 maven 依赖

<!-- web支持 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf模板引擎 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- aop依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- lombok插件 -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>

我们引入了 aop 切面所需依赖,我们的数据字典也是基于 aop 切面实现的

2、创建实体类

用户信息表 SysUserInfo.java:

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.zyxx.common.annotation.Dict;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;import java.io.Serializable;/*** <p>* 用户信息表* </p>** @author lizhou* @since 2020-07-06*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user_info")
@ApiModel(value="SysUserInfo对象", description="用户信息表")
public class SysUserInfo extends Model<SysUserInfo> {@ApiModelProperty(value = "ID")@TableId(value = "id", type = IdType.AUTO)private Long id;@ApiModelProperty(value = "登录账号")@TableField("account")private String account;@ApiModelProperty(value = "登录密码")@TableField("password")private String password;@ApiModelProperty(value = "姓名")@TableField("name")private String name;@ApiModelProperty(value = "性别(0--未知1--男2--女)")@TableField("sex")@Dict(dictCode = "sex")private Integer sex;@ApiModelProperty(value = "状态(0--正常1--冻结)")@TableField("status")@Dict(dictCode = "status")private Integer status;
}

3、返回结果通用实体类

返回结果通用实体类 LayTableResult.java:

import lombok.Getter;
import lombok.Setter;import java.util.List;/*** @param <T> 返回的实体类* @author lizhou* @描述 后台返回给LayUI的数据格式*/
@Getter
@Setter
public class LayTableResult<T> {/*** 接口状态*/private Integer code;/*** 提示信息*/private String msg;/*** 接口数据长度*/private Long count;/*** 接口数据*/private List<T> data;/*** 无参构造函数*/public LayTableResult() {super();}/*** 返回数据给表格*/public LayTableResult(Long count, List<T> data) {super();this.count = count;this.data = data;this.code = 0;}
}

由于我用的是 layui 前端框架,我写了一个返给 layui 表格的通用实体类,这是在实现数据字典需要用到的,判断响应返回实体类的类型来判断是否需要注入字典

四、开发实现

1、创建自定义注解

我们创建一个自定义注解 @Dict 来实现数据字典

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** 数据字典注解** @author Tellsea* @date 2020/6/23*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dict {/*** 字典类型** @return*/String dictCode();/*** 返回属性名** @return*/String dictText() default "";
}

2、注解实现

我们使用 aop 切面来实现什么的自定义注解 @Dict

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zyxx.common.annotation.Dict;
import com.zyxx.common.utils.LayTableResult;
import com.zyxx.common.utils.ObjConvertUtils;
import com.zyxx.sbm.entity.SysDictDetail;
import com.zyxx.sbm.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;/*** 数据字典切面** @author Tellsea* @date 2020/6/23*/
@Aspect
@Component
@Slf4j
public class DictAspect {/*** 字典后缀*/private static String DICT_TEXT_SUFFIX = "Text";@Autowiredprivate SysDictService sysDictService;/*** 切点,切入 controller 包下面的所有方法*/@Pointcut("execution( * com.zyxx.*.controller.*.*(..))")public void dict() {}@Around("dict()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long time1 = System.currentTimeMillis();Object result = pjp.proceed();long time2 = System.currentTimeMillis();log.debug("获取JSON数据 耗时:" + (time2 - time1) + "ms");long start = System.currentTimeMillis();this.parseDictText(result);long end = System.currentTimeMillis();log.debug("解析注入JSON数据  耗时" + (end - start) + "ms");return result;}private void parseDictText(Object result) {if (result instanceof LayTableResult) {List<JSONObject> items = new ArrayList<>();LayTableResult rr = (LayTableResult) result;if (rr.getCount() > 0) {List<?> list = (List<?>) rr.getData();for (Object record : list) {ObjectMapper mapper = new ObjectMapper();String json = "{}";try {// 解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormatjson = mapper.writeValueAsString(record);} catch (JsonProcessingException e) {log.error("Json解析失败:" + e);}JSONObject item = JSONObject.parseObject(json);// 解决继承实体字段无法翻译问题for (Field field : ObjConvertUtils.getAllFields(record)) {//解决继承实体字段无法翻译问题// 如果该属性上面有@Dict注解,则进行翻译if (field.getAnnotation(Dict.class) != null) {// 拿到注解的dictDataSource属性的值String dictType = field.getAnnotation(Dict.class).dictCode();// 拿到注解的dictText属性的值String text = field.getAnnotation(Dict.class).dictText();//获取当前带翻译的值String key = String.valueOf(item.get(field.getName()));//翻译字典值对应的text值String textValue = translateDictValue(dictType, key);// DICT_TEXT_SUFFIX的值为,是默认值:// public static final String DICT_TEXT_SUFFIX = "_dictText";log.debug("字典Val: " + textValue);log.debug("翻译字典字段:" + field.getName() + DICT_TEXT_SUFFIX + ": " + textValue);//如果给了文本名if (!StringUtils.isBlank(text)) {item.put(text, textValue);} else {// 走默认策略item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);}}// date类型默认转换string格式化日期if ("java.util.Date".equals(field.getType().getName())&& field.getAnnotation(JsonFormat.class) == null&& item.get(field.getName()) != null) {SimpleDateFormat aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));}}items.add(item);}rr.setData(items);}}}/*** 翻译字典文本** @param dictType* @param key* @return*/private String translateDictValue(String dictType, String key) {if (ObjConvertUtils.isEmpty(key)) {return null;}StringBuffer textValue = new StringBuffer();String[] keys = key.split(",");for (String k : keys) {if (k.trim().length() == 0) {continue;}/*** 根据 dictCode 和 code 查询字典值,例如:dictCode:sex,code:1,返回:男* 应该放在redis,提高响应速度*/SysDictDetail dictData = sysDictService.getDictDataByTypeAndValue(dictType, key);if (dictData.getName() != null) {if (!"".equals(textValue.toString())) {textValue.append(",");}textValue.append(dictData.getName());}log.info("数据字典翻译: 字典类型:{},当前翻译值:{},翻译结果:{}", dictType, k.trim(), dictData.getName());}return textValue.toString();}
}

上面用到的 ObjConvertUtils 类

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;/*** 对象转换工具类** @Author Lizhou*/
@SuppressWarnings("ALL")
public class ObjConvertUtils {/*** 获取类的所有属性,包括父类** @param object* @return*/public static Field[] getAllFields(Object object) {Class<?> clazz = object.getClass();List<Field> fieldList = new ArrayList<>();while (clazz != null) {fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));clazz = clazz.getSuperclass();}Field[] fields = new Field[fieldList.size()];fieldList.toArray(fields);return fields;}public static boolean isEmpty(Object object) {if (object == null) {return (true);}if ("".equals(object)) {return (true);}if ("null".equals(object)) {return (true);}return (false);}
}

3、注解使用

我们只需要在实体类的属性上加入我们实现的自定义注解即可

@ApiModelProperty(value = "性别(0--未知1--男2--女)")
@TableField("sex")
@Dict(dictCode = "sex")
private Integer sex;@ApiModelProperty(value = "状态(0--正常1--冻结)")
@TableField("status")
@Dict(dictCode = "status")
private Integer status;

我们对 sex,status 都加入了 @Dict(dictCode = “”) 注解,那么我们在获取用户信息的时候,就能获取到对应的字典值了

五、测试

1、编写 API 查询

我们在 controller 层开放一个 API 实现查询用户列表

/*** 分页查询*/
@PostMapping("list")
@ResponseBody
public LayTableResult list(Integer page, Integer limit, SysUserInfo userInfo) {QueryWrapper<SysUserInfo> queryWrapper = new QueryWrapper<>();if (StringUtils.isNotBlank(userInfo.getName())) {queryWrapper.like("name", userInfo.getName());}if (null != userInfo.getSex()) {queryWrapper.eq("sex", userInfo.getSex());}if (null != userInfo.getStatus()) {queryWrapper.eq("status", userInfo.getStatus());}queryWrapper.orderByDesc("create_time");IPage<SysUserInfo> iPage = sysUserInfoService.page(new Page<>(page, limit), queryWrapper);return new LayTableResult<>(iPage.getTotal(), iPage.getRecords());
}

注意: 这里我们使用了 LayTableResult 作为相应实体类,与上面我们编写的返回通用实体类是一致的,必须一直,才能实现数据字典功能

2、调用 API

返回结果如下:

{"code": 0,"msg": null,"count": 3,"data": [{"id": 2,"account": "15286779045","name": "周杰伦","sex": 1,"sexText": "男","status": 0,"statusText": "正常"}, {"id": 1,"name": "超级管理员","account": "15286779044","sex": 1,"sexText": "男","status": 0,"statusText": "正常"}]
}

可以看出,返回的数据中,多出了 sexText,statusText,两个属性,也就证明我们的字典功能已经实现成功

六、总结

1、优点

1、在一定程度上,通过系统维护人员即可改变系统的行为(功能),不需要开发人员的介入。使得系统的变化更快,能及时响应客户和市场的需求。
2、提高了系统的灵活性、通用性,减少了主体和属性的耦合度 3、简化了主体类的业务逻辑 4、
能减少对系统程序的改动,使数据库、程序和页面更稳定。特别是数据量大的时候,能大幅减少开发工作量
5、使数据库表结构和程序结构条理上更清楚,更容易理解,在可开发性、可扩展性、可维护性、系统强壮性上都有优势。

2、缺点

1、数据字典是通用的设计,在系统效率上会低一些。
2、程序算法相对复杂一些。
3、对于开发人员,需要具备一定抽象思维能力,所以对开发人员的要求较高。

3、优化

我们的数据字典数据应该存放在 redis 中,减少与数据库的交互次数,提高响应速度

如您在阅读中发现不足,欢迎留言!!!

【SpringBoot】24、SpringBoot中实现数据字典相关推荐

  1. 13.9 SpringBoot集成Swagger2中遇到的问题

    13.9 SpringBoot集成Swagger2中遇到的问题 我们在使用SpringBoot集成Swagger2中,访问:http://127.0.0.1:8188/swagger-ui.html ...

  2. spring-boot的spring-cache中的扩展redis缓存的ttl和key名

    原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...

  3. SpringBoot RESTful 应用中的异常处理小结

    SpringBoot RESTful 应用中的异常处理小结 参考文章: (1)SpringBoot RESTful 应用中的异常处理小结 (2)https://www.cnblogs.com/Zomb ...

  4. Springboot Web应用中服务器配置参数ServerProperties的加载

    #概述 Springboot配置文件中以server开头的项表示服务器的配置参数,这一点从字面意义即可直观理解,这些参数,包括端口,路径设置,SSL配置参数等等.具体有哪些参数,从源代码的角度上,可以 ...

  5. 解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for service x.x.x on consumer

    解决springboot整合dubbo中No provider available from registry 127.0.0.1:2181 for service x.x.x on consumer ...

  6. Springboot 启动命令中–spring.config.location不生效问题

    Springboot 启动命令中–spring.config.location不生效问题 1.问题: 项目启动时,有的服务可以通过–spring.config.location读取外置的配置文件,有的 ...

  7. SpringBoot系列-- SpringBoot中使用lombok简化开发

    目录 1. 项目环境 2.添加依赖和安装插件 3. 实体类常用注解总结 3.1 @Setter.@Getter 3.1.1 注解在属性上 3.1.2 注解在类上 3.1.3 注解对静态属性和final ...

  8. Java|Kotlin, SpringBoot从配置文件中读取@KafkaListener参数配置

    springboot从配置文件中设置@KafkaListener参数配置 在java中使用占位符#{'${kakfa.topics}'}来进行参数注入 @KafkaListener(topics = ...

  9. SpringBoot+Vue项目中实现登录验证码校验

    SpringBoot+Vue项目中实现登录验证码校验 在各大项目中,为保证数据的安全性,通常在登录页面加入验证码校验,以防止爬虫带来的数据泄露危机.本文将介绍在前后端分离的项目中,怎样实现图形验证码校 ...

  10. Springboot加密配置文件中数据库密码的两种解决方案

    Springboot 加密配置文件中数据库密码的两种解决方案 第一种:jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring Boot 项目中,并提供了自动 ...

最新文章

  1. 方法apply作用于对象sort时失败_浅析call、apply 与 bind
  2. dom操作表格示例(dom创建表格)
  3. 基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理
  4. python 字符串格式符
  5. 苹果笔记本怎么找文件夹_如何在苹果笔记本中找出 “~/Library/Preferences/” 文件夹?...
  6. Java-NIO(三):直接缓冲区与非直接缓冲区
  7. springboot中的ApplicationRunner 接口
  8. matlab贝塞尔函数的根,matlab画贝塞尔函数根分布的曲线图.doc
  9. SQL必知必会-视图
  10. jmeter java性能_使用JMeter进行性能测试(Java请求)
  11. 佐藤hiroko-爱拯救了我(步之物语)
  12. Linux SD卡驱动开发(三) —— SD 卡驱动分析CORE篇
  13. 计算机设备维修与及日常保养,电脑主机日常的维护保养计划
  14. 职称论文发表格式要求
  15. 老台式计算机怎么调亮度,老台式电脑亮度怎么调
  16. 苹果 CEO 为什么选中了何同学?
  17. 模仿斗地主玩法实现扑克牌的分发
  18. 黑龙江软件第三方检测机构/软件登记(退税)测试报告
  19. 信息学奥赛一本通1258:【例9.2】数字金字塔题解
  20. 为什么Hashtable ConcurrentHashmap不支持key或者value为null

热门文章

  1. HTML5网页设计基础——播放器图标
  2. 【游戏引擎Easy2D】场景和文本,不同的输出方式
  3. 以后华为鸿蒙也要越狱了,玩机福音:华为 EMUI 升级鸿蒙之后依然可使用太极运行 Xposed...
  4. 基于python微信群聊机器人开题报告
  5. PSO 粒子群优化算法
  6. JAVA学习心得——DBUtil工具类
  7. 公司账号密码、通信录泄露屡见不鲜,肆意流淌的敏感信息:WEB安全基础入门—信息泄露漏洞
  8. 你知道如何提升JSON.stringify()的性能吗?
  9. 英语在线听力翻译器_英语听力翻译软件下载_英语听力翻译2020官网下载地址_开心技术乐园...
  10. 01使用Python分析科比生涯数据