SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼
我們?cè)谌粘5捻?xiàng)目開(kāi)發(fā)中,對(duì)于數(shù)據(jù)字典肯定不模糊,它幫助了我們更加方便快捷地進(jìn)行開(kāi)發(fā),下面一起來(lái)看看在 SpringBoot 中如何實(shí)現(xiàn)數(shù)據(jù)字典功能的
一、簡(jiǎn)介
1、定義
數(shù)據(jù)字典是指對(duì)數(shù)據(jù)的數(shù)據(jù)項(xiàng)、數(shù)據(jù)結(jié)構(gòu)、數(shù)據(jù)流、數(shù)據(jù)存儲(chǔ)、處理邏輯等進(jìn)行定義和描述,其目的是對(duì)數(shù)據(jù)流程圖中的各個(gè)元素做出詳細(xì)的說(shuō)明,使用數(shù)據(jù)字典為簡(jiǎn)單的建模項(xiàng)目。簡(jiǎn)而言之,數(shù)據(jù)字典是描述數(shù)據(jù)的信息集合,是對(duì)系統(tǒng)中使用的所有數(shù)據(jù)元素的定義的集合。
數(shù)據(jù)字典(Data dictionary)是一種用戶(hù)可以訪問(wèn)的記錄數(shù)據(jù)庫(kù)和應(yīng)用程序元數(shù)據(jù)的目錄。主動(dòng)數(shù)據(jù)字典是指在對(duì)數(shù)據(jù)庫(kù)或應(yīng)用程序結(jié)構(gòu)進(jìn)行修改時(shí),其內(nèi)容可以由DBMS自動(dòng)更新的數(shù)據(jù)字典。被動(dòng)數(shù)據(jù)字典是指修改時(shí)必須手工更新其內(nèi)容的數(shù)據(jù)字典。
2、理解
數(shù)據(jù)字典是一種通用的程序設(shè)計(jì)思想,將主體與分支存于兩張數(shù)據(jù)表中,他們之間靠著唯一的 code 相互聯(lián)系,且 code 是唯一存在的,分支依附主體而存在,每一條分支都有它唯一對(duì)應(yīng)的屬性值
例如:性別(sex),分為(0–保密1–男2–女),那么數(shù)據(jù)字典的設(shè)計(jì)就應(yīng)該是
主表:
{
"code": "sex",
"name": "性別"
}
副表:
[{
"dictCode": "sex",
"code": "0",
"text": "保密"
},
{
"dictCode": "sex",
"code": "1",
"text": "男"
},
{
"dictCode": "sex",
"code": "2",
"text": "女"
}
]
那么我們?cè)谑褂脭?shù)據(jù)字典的時(shí)候,只需要知道 dictCode,再使用 code 找到唯一的字典值
二、數(shù)據(jù)表設(shè)計(jì)
1、數(shù)據(jù)表設(shè)計(jì)
主表:
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 '名稱(chēng)', descript varchar(64) comment '描述', status tinyint(1) default 0 comment '狀態(tài)(0--正常1--凍結(jié))', create_time datetime comment '創(chuàng)建時(shí)間', create_user bigint(20) comment '創(chuàng)建人', del_flag tinyint(1) default 0 comment '刪除狀態(tài)(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 '名稱(chēng)', primary key (id) ) type = InnoDB; alter table sys_dict_detail comment '字典配置表';
它們的關(guān)系如圖所示:

2、數(shù)據(jù)字典配置

三、開(kāi)發(fā)前戲
1、引入 maven 依賴(lài)
<!-- 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依賴(lài) --> <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 切面所需依賴(lài),我們的數(shù)據(jù)字典也是基于 aop 切面實(shí)現(xiàn)的
2、創(chuàng)建實(shí)體類(lèi)
用戶(hù)信息表 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>
* 用戶(hù)信息表
* </p>
*
* @author lizhou
* @since 2020-07-06
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user_info")
@ApiModel(value="SysUserInfo對(duì)象", description="用戶(hù)信息表")
public class SysUserInfo extends Model<SysUserInfo> {
@ApiModelProperty(value = "ID")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "登錄賬號(hào)")
@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 = "狀態(tài)(0--正常1--凍結(jié))")
@TableField("status")
@Dict(dictCode = "status")
private Integer status;
}
3、返回結(jié)果通用實(shí)體類(lèi)
返回結(jié)果通用實(shí)體類(lèi) LayTableResult.java:
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* @param <T> 返回的實(shí)體類(lèi)
* @author lizhou
* @描述 后臺(tái)返回給LayUI的數(shù)據(jù)格式
*/
@Getter
@Setter
public class LayTableResult<T> {
/**
* 接口狀態(tài)
*/
private Integer code;
/**
* 提示信息
*/
private String msg;
/**
* 接口數(shù)據(jù)長(zhǎng)度
*/
private Long count;
/**
* 接口數(shù)據(jù)
*/
private List<T> data;
/**
* 無(wú)參構(gòu)造函數(shù)
*/
public LayTableResult() {
super();
}
/**
* 返回?cái)?shù)據(jù)給表格
*/
public LayTableResult(Long count, List<T> data) {
super();
this.count = count;
this.data = data;
this.code = 0;
}
}
由于我用的是 layui 前端框架,我寫(xiě)了一個(gè)返給 layui 表格的通用實(shí)體類(lèi),這是在實(shí)現(xiàn)數(shù)據(jù)字典需要用到的,判斷響應(yīng)返回實(shí)體類(lèi)的類(lèi)型來(lái)判斷是否需要注入字典
四、開(kāi)發(fā)實(shí)現(xiàn)
1、創(chuàng)建自定義注解
我們創(chuàng)建一個(gè)自定義注解 @Dict 來(lái)實(shí)現(xiàn)數(shù)據(jù)字典
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 數(shù)據(jù)字典注解
*
* @author Tellsea
* @date 2020/6/23
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dict {
/**
* 字典類(lèi)型
*
* @return
*/
String dictCode();
/**
* 返回屬性名
*
* @return
*/
String dictText() default "";
}
2、注解實(shí)現(xiàn)
我們使用 aop 切面來(lái)實(shí)現(xiàn)什么的自定義注解 @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;
/**
* 數(shù)據(jù)字典切面
*
* @author Tellsea
* @date 2020/6/23
*/
@Aspect
@Component
@Slf4j
public class DictAspect {
/**
* 字典后綴
*/
private static String DICT_TEXT_SUFFIX = "Text";
@Autowired
private SysDictService sysDictService;
/**
* 切點(diǎn),切入 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數(shù)據(jù) 耗時(shí):" + (time2 - time1) + "ms");
long start = System.currentTimeMillis();
this.parseDictText(result);
long end = System.currentTimeMillis();
log.debug("解析注入JSON數(shù)據(jù) 耗時(shí)" + (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注解解析不了的問(wèn)題詳見(jiàn)SysAnnouncement類(lèi)的@JsonFormat
json = mapper.writeValueAsString(record);
} catch (JsonProcessingException e) {
log.error("Json解析失?。? + e);
}
JSONObject item = JSONObject.parseObject(json);
// 解決繼承實(shí)體字段無(wú)法翻譯問(wèn)題
for (Field field : ObjConvertUtils.getAllFields(record)) {
//解決繼承實(shí)體字段無(wú)法翻譯問(wèn)題
// 如果該屬性上面有@Dict注解,則進(jìn)行翻譯
if (field.getAnnotation(Dict.class) != null) {
// 拿到注解的dictDataSource屬性的值
String dictType = field.getAnnotation(Dict.class).dictCode();
// 拿到注解的dictText屬性的值
String text = field.getAnnotation(Dict.class).dictText();
//獲取當(dāng)前帶翻譯的值
String key = String.valueOf(item.get(field.getName()));
//翻譯字典值對(duì)應(yīng)的text值
String textValue = translateDictValue(dictType, key);
// DICT_TEXT_SUFFIX的值為,是默認(rèn)值:
// 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 {
// 走默認(rèn)策略
item.put(field.getName() + DICT_TEXT_SUFFIX, textValue);
}
}
// date類(lèi)型默認(rèn)轉(zhuǎn)換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;
}
/**
* 根據(jù) dictCode 和 code 查詢(xún)字典值,例如:dictCode:sex,code:1,返回:男
* 應(yīng)該放在redis,提高響應(yīng)速度
*/
SysDictDetail dictData = sysDictService.getDictDataByTypeAndValue(dictType, key);
if (dictData.getName() != null) {
if (!"".equals(textValue.toString())) {
textValue.append(",");
}
textValue.append(dictData.getName());
}
log.info("數(shù)據(jù)字典翻譯: 字典類(lèi)型:{},當(dāng)前翻譯值:{},翻譯結(jié)果:{}", dictType, k.trim(), dictData.getName());
}
return textValue.toString();
}
}
3、注解使用
我們只需要在實(shí)體類(lèi)的屬性上加入我們實(shí)現(xiàn)的自定義注解即可
@ApiModelProperty(value = "性別(0--未知1--男2--女)")
@TableField("sex")
@Dict(dictCode = "sex")
private Integer sex;
@ApiModelProperty(value = "狀態(tài)(0--正常1--凍結(jié))")
@TableField("status")
@Dict(dictCode = "status")
private Integer status;
我們對(duì) sex,status 都加入了 @Dict(dictCode = “”) 注解,那么我們?cè)讷@取用戶(hù)信息的時(shí)候,就能獲取到對(duì)應(yīng)的字典值了
五、測(cè)試
1、編寫(xiě) API 查詢(xún)
我們?cè)?controller 層開(kāi)放一個(gè) API 實(shí)現(xiàn)查詢(xún)用戶(hù)列表
/**
* 分頁(yè)查詢(xún)
*/
@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 作為相應(yīng)實(shí)體類(lèi),與上面我們編寫(xiě)的返回通用實(shí)體類(lèi)是一致的,必須一直,才能實(shí)現(xiàn)數(shù)據(jù)字典功能
2、調(diào)用 API
返回結(jié)果如下:
{
"code": 0,
"msg": null,
"count": 3,
"data": [{
"id": 2,
"account": "15286779045",
"name": "周杰倫",
"sex": 1,
"sexText": "男",
"status": 0,
"statusText": "正常"
}, {
"id": 1,
"name": "超級(jí)管理員",
"account": "15286779044",
"sex": 1,
"sexText": "男",
"status": 0,
"statusText": "正常"
}]
}
可以看出,返回的數(shù)據(jù)中,多出了 sexText,statusText,兩個(gè)屬性,也就證明我們的字典功能已經(jīng)實(shí)現(xiàn)成功
六、總結(jié)
1、優(yōu)點(diǎn)
1、在一定程度上,通過(guò)系統(tǒng)維護(hù)人員即可改變系統(tǒng)的行為(功能),不需要開(kāi)發(fā)人員的介入。使得系統(tǒng)的變化更快,能及時(shí)響應(yīng)客戶(hù)和市場(chǎng)的需求。
2、提高了系統(tǒng)的靈活性、通用性,減少了主體和屬性的耦合度 3、簡(jiǎn)化了主體類(lèi)的業(yè)務(wù)邏輯 4、
能減少對(duì)系統(tǒng)程序的改動(dòng),使數(shù)據(jù)庫(kù)、程序和頁(yè)面更穩(wěn)定。特別是數(shù)據(jù)量大的時(shí)候,能大幅減少開(kāi)發(fā)工作量
5、使數(shù)據(jù)庫(kù)表結(jié)構(gòu)和程序結(jié)構(gòu)條理上更清楚,更容易理解,在可開(kāi)發(fā)性、可擴(kuò)展性、可維護(hù)性、系統(tǒng)強(qiáng)壯性上都有優(yōu)勢(shì)。
2、缺點(diǎn)
1、數(shù)據(jù)字典是通用的設(shè)計(jì),在系統(tǒng)效率上會(huì)低一些。
2、程序算法相對(duì)復(fù)雜一些。
3、對(duì)于開(kāi)發(fā)人員,需要具備一定抽象思維能力,所以對(duì)開(kāi)發(fā)人員的要求較高。
3、優(yōu)化
我們的數(shù)據(jù)字典數(shù)據(jù)應(yīng)該存放在 redis 中,減少與數(shù)據(jù)庫(kù)的交互次數(shù),提高響應(yīng)速度
到此這篇關(guān)于SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 數(shù)據(jù)字典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java集合基礎(chǔ)知識(shí) List/Set/Map詳解
這篇文章主要介紹了Java集合基礎(chǔ)知識(shí) List/Set/Map,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java打印斐波那契前N項(xiàng)的實(shí)現(xiàn)示例
這篇文章主要介紹了Java打印斐波那契前N項(xiàng)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
在jmeter的beanshell中用java獲取系統(tǒng)當(dāng)前時(shí)間的簡(jiǎn)單實(shí)例
這篇文章介紹了在jmeter的beanshell中用java獲取系統(tǒng)當(dāng)前時(shí)間的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-09-09
java實(shí)現(xiàn)二維碼生成功能詳細(xì)示例
這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)二維碼生成功能的相關(guān)資料,隨著信息化時(shí)代的到來(lái),二維碼作為一種信息傳遞的工具,越來(lái)越受到人們的歡迎,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
springboot配置允許跨域訪問(wèn)代碼實(shí)例
這篇文章主要介紹了springboot配置允許跨域訪問(wèn)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Java中的Set接口實(shí)現(xiàn)類(lèi)HashSet和LinkedHashSet詳解
這篇文章主要介紹了Java中的Set接口實(shí)現(xiàn)類(lèi)HashSet和LinkedHashSet詳解,Set接口和java.util.List接口一樣,同樣繼承自Collection接口,它與Collection接口中的方法基本一致,并沒(méi)有對(duì)Collection接口進(jìn)行功能上的擴(kuò)充,只是比Collection接口更加嚴(yán)格了,需要的朋友可以參考下2024-01-01
如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件
這篇文章主要介紹了如何在spring官網(wǎng)查找XML基礎(chǔ)配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
SpringBoot內(nèi)置Tomcat啟動(dòng)方式
Spring Boot通過(guò)啟動(dòng)類(lèi)上的@EnableAutoConfiguration注解,自動(dòng)生成并加載ServletWebServerFactoryAutoConfiguration類(lèi),該類(lèi)通過(guò)@Import注解導(dǎo)入TomcatServletWebServerFactory類(lèi),該類(lèi)在getWebServer()方法中創(chuàng)建并啟動(dòng)TomcatServletWebServer對(duì)象2024-12-12

