标准代码及数据字典的实现

SpringBoot中实现数据字典的示例代码

我们在日常的项目开发中,对于数据字典肯定不模糊,它帮助了我们更加方便快捷地进行开发,下面一起来看看在 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 '字典配置表';

三、开发

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.xxxx.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;
 
/**
 * 用户信息表
 */
@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> 返回的实体类
 */
@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;
  }
}

四、开发实现

1、创建自定义注解

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * 数据字典注解
 */
@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.xxxx.common.annotation.Dict;
import com.xxxx.common.utils.LayTableResult;
import com.xxxx.common.utils.ObjConvertUtils;
import com.xxxx.sbm.entity.SysDictDetail;
import com.xxxx.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;
 
/**
 * 数据字典切面
 */
@Aspect
@Component
@Slf4j
public class DictAspect {
 
	/**
	* 字典后缀
	*/
  private static String DICT_TEXT_SUFFIX = "Text";
 
  @Autowired
  private 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类的@JsonFormat
            json = 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

public class ObjConvertUtils {

  /**
  * 获取类的所有属性,包括父类
  * @param object
  * @return
  */
  public static Field[] getAllFields(Object object) {
    Class<?> clazz = object.getClass();
    List 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": "urqiuiop8789",
		"name": "jack",
		"sex": 1,
		"sexText": "男",
		"status": 0,
		"statusText": "正常"
	}, {
		"id": 1,
		"name": "管理员",
		"account": "15286779044",
		"sex": 1,
		"sexText": "男",
		"status": 0,
		"statusText": "正常"
	}]
}

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

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

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值