字段字典翻译
# 说明
字典翻译一般用于接口响应和导入导出时,框架增加了一个注解@Dict
有两个参数
dictCode:字典编码,可选参数,如果填写此参数,那么将使用系统字典表里的翻译数据
custom,可选参数,如果填写此参数,那么将当前参数定义的翻译数据
如果两个参数都填写,将使用dictCode
参数
//使用自定义字典进行翻译,意思是直接写死在代码里的
@Dict(custom = {@DictDetail(value = "0", label = "正常"), @DictDetail(value = "1", label = "禁用")})
//使用系统里的字典表进行翻译,当然了,实际取的是redis缓存里的
@Dict(dictCode = "sys_normal_disable", custom = {})
下面分两部分讲解
# 接口响应
接口响应就是在响应给前端json时,自动增加一个字段对象,用来展示翻译
注:使用jackson序列化完成
# 使用例程
package cn.daenx.myadmin.test.domain.po;
import cn.daenx.myadmin.common.annotation.Dict;
import cn.daenx.myadmin.common.annotation.DictDetail;
import cn.daenx.myadmin.common.vo.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 测试数据
*/
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "test_data")
public class TestData extends BaseEntity implements Serializable {
@TableId(value = "id", type = IdType.ASSIGN_UUID)
private String id;
@TableField(value = "title")
private String title;
@TableField(value = "content")
private String content;
/**
* 类型,0=民生,1=科技
*/
@TableField(value = "type")
//使用自定义字典进行翻译,意思是直接写死在代码里的
@Dict(custom = {@DictDetail(value = "0", label = "民生"), @DictDetail(value = "1", label = "科技"), @DictDetail(value = "2", label = "农业"), @DictDetail(value = "3", label = "其他")})
// @Dict(dictCode = "test_data_type", custom = {})
private String type;
/**
* 备注
*/
@TableField(value = "remark")
private String remark;
/**
* 状态,0=正常,1=禁用
*/
@TableField(value = "`status`")
// @Dict(custom = {@DictDetail(value = "0", label = "正常"), @DictDetail(value = "1", label = "禁用")})
//使用系统字典表里的翻译数据,推荐
@Dict(dictCode = "sys_normal_disable", custom = {})
private String status;
}
# 效果
{
"code": 200,
"success": true,
"msg": "操作成功",
"data": {
"createId": "1",
"createTime": "2023-06-03 10:56:21",
"updateId": "1",
"updateTime": "2023-06-03 10:58:26",
"isDelete": 0,
"createName": null,
"updateName": null,
"createDept": null,
"id": "8f05e1af476e3537e796c966ff4b7b53",
"title": "阿萨德",
"content": "阿萨德",
"type": "0",
//这里就是效果
"typeDict": {
"label": "民生",
"value": "0"
},
"remark": "4",
"status": "0",
//这里就是效果
"statusDict": {
"listClass": "primary",
"cssClass": null,
"remark": "正常状态",
"label": "正常",
"value": "0",
"status": "0"
}
},
"timestamp": 1686822129198
}
# 导入导出
就是在导出xlsx时,自动将字段替换为字典里的label,例如 性别,导出的xlsx时,表格里显示的是男
而不是1
导入时,框架会自动将男
转换为1
然后入库
注意,需要在@ExcelProperty
注解中指定转换器
为ExcelConverter
@ExcelProperty(value = "状态", converter = ExcelConverter.class)
注:导入和导出使用了阿里巴巴的easyexcel
# 使用例程
package cn.daenx.myadmin.test.domain.dto;
import cn.daenx.myadmin.common.annotation.Dict;
import cn.daenx.myadmin.common.annotation.DictDetail;
import cn.daenx.myadmin.framework.excel.ExcelConverter;
import cn.daenx.myadmin.common.vo.BaseDto;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
//导出时忽略没有@ExcelProperty的字段
@ExcelIgnoreUnannotated
public class TestDataPageDto extends BaseDto {
@ExcelProperty(value = "id")
private String id;
@ExcelProperty(value = "标题")
private String title;
@ExcelProperty(value = "内容")
private String content;
@ExcelProperty(value = "类型", converter = ExcelConverter.class)
//使用自定义字典进行翻译,意思是直接写死在代码里的
@Dict(custom = {@DictDetail(value = "0", label = "民生"), @DictDetail(value = "1", label = "科技"), @DictDetail(value = "2", label = "农业"), @DictDetail(value = "3", label = "其他")})
// @Dict(dictCode = "test_data_type", custom = {})
private String type;
@ExcelProperty(value = "状态", converter = ExcelConverter.class)
// @Dict(custom = {@DictDetail(value = "0", label = "正常"), @DictDetail(value = "1", label = "禁用")})
//使用系统字典表里的翻译数据,推荐
@Dict(dictCode = "sys_normal_disable", custom = {})
private String status;
@ExcelProperty(value = "备注")
private String remark;
}
# 效果
上次更新: 2024/09/20, 10:00:15