# simple-dictionary
**Repository Path**: open-easy/simple-dictionary
## Basic Information
- **Project Name**: simple-dictionary
- **Description**: 解决方案:数据字典翻译,数据字典转换,数据字典映射
- **Primary Language**: Java
- **License**: MulanPSL-2.0
- **Default Branch**: master
- **Homepage**: https://gitee.com/Goslee/simple-dictionary
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 0
- **Created**: 2025-04-30
- **Last Updated**: 2025-10-17
## Categories & Tags
**Categories**: Uncategorized
**Tags**: 数据字典, 字段翻译, 键值对, SpringBoot, 业务字段翻译
## README
## 🛠️simple-dictionary
#### 一个轻量级的数据字典翻译工具
🍬Make it simple, make it simple.
-------------------------------------------------------------------------------
### 📚背景概述
针对小中大型企业设计的一套数据字典解决方案。
1、当你看到前端和后端的数据字典不统一时,前后端都需要回忆当时的需求是什么样的。
2、当系统中的字典很多有很杂的时候,铺天盖地的数据字典枚举随处可见。
3、当你看到后台管理人员, 业务人员想要知道这个字段所有枚举时,他们都需要来询问前后端或者翻以前的需求文档才能得知。
那么就需要用数据字典来统一管理数据字典,无论是前后端还是业务人员都只需要找到一个地方就能统一规范,因此,该插件就应运而生。
### 🧬系统需求
  
### 📐当前版本

### 🍊快速开始
#### 1、需要建立以下数据库表(必要时需要建立索引)
此表的创建以下是关键字段,其他业务字段可以自行添加
```sql
create table if not exists bas_dictionary
(
id bigint not null primary key,
code varchar(50),
value varchar(50),
description varchar(50),
order_field smallint,
server_name varchar(50)
);
comment on table bas_dictionary is '数据字典表';
comment on column bas_dictionary.code is '数据字典类型code';
comment on column bas_dictionary.value is '值';
comment on column bas_dictionary.description is '中文翻译';
comment on column bas_dictionary.order_field is '排序';
comment on column bas_dictionary.server_name is '服务名';
```
#### 2、需要在在需要使用的模块引入pom依赖
> 注意:暂未发布中央仓库
> 注意:暂未发布中央仓库
> 注意:暂未发布中央仓库
```xml
io.gitee.goslee
simple-dictionary
1.0.0
```
#### 3、二选一使用方式
##### 3.1、使用注解方式
在controller上添加注解
```java
@RestController
@RequestMapping("/test")
public class TestController {
@PostMapping("/testinfo")
@HandleDict
public UserInfo userList(@Valid @RequestBody VO vo) {
return service.userList(vo);
}
}
```
在UserInfo实体类中对需要添加的字段加上注解DictCode,statusDic是指定的翻译字段名
```java
@DictCode(field = "statusDic", code = "status")
private String status;
private String statusDic;
```
字典表中添加字典
```sql
INSERT INTO "bas_dictionary" ("id", "code", "description", "value", "order_field", "server_name")
VALUES ((select max(id) from bas_dictionary) + 1, 'status', '成功', 'success', 1, 'user-server');
INSERT INTO "bas_dictionary" ("id", "code", "description", "value", "order_field", "server_name")
VALUES ((select max(id) from bas_dictionary) + 1, 'status', '失败', 'fail', 2, 'user-server');
```
最终执行结果
```json
{
"status": "success",
"statusDic": "成功"
}
```
##### 3.2、使用util编码方式
```java
import com.sdictionary.util.DictUtil;
UserInfo userInfo = new UserInfo();
userInfo.setStatus("success");
DictUtil.convert(userInfo);
//放入类型支持所有类型
sout(userInfo);
//结果为
//{"status": "success","statusDic": "成功"}
String value = DictUtil.get("status", "success");//也能获取字典值
LinkedHashMap resultMap = DictUtil.getDictMap("status");//也能获取字典值
```
##### 3.3、扩展方式(可选)
很多项目会统一包装返回结果,我拿一个例子来说,在统一包装返回路径时进行处理数据字典翻译,会非常方便
其中:BaseResult是项目中统一返回结果的实体类,DictUtil是数据字典翻译工具类,需要引入依赖
```java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author fengbaiyu
* @date 2025/4/23
*/
@RestControllerAdvice(basePackages = {"你的controller包路径"})
@EnableAutoConfiguration
@Slf4j
@Order(100)
public class ControllerRespAdviceConfig implements ResponseBodyAdvice