1. 数据翻译
springboot 项目开发中常见到的数据翻译包括:
枚举翻译、字典表翻译、外键翻译、级联字段翻译、远程调用翻译等。
2. 数据来源
- 枚举代码(枚举)
- 数据表(字典表、关联表)
- 自定义方法(本地处理,跨服务查询、第三方API查询等)
核心注解 | 应用场景 |
@DictType | @DictType(value = "sys_is_used", label = "使用状态") 说明:枚举字典定义标识 |
@TransEnum | @TransEnum(dictType = "类型code", source = "变量名") 说明:使用枚举翻译 |
@TransDict | @TransDict(dictType = "类型code", source = "源变量名") 说明:优先使用枚举翻译,再使用字典表翻译; 需要自定义实现接口 TransDictHandler,用于读取字典表翻译 |
@TransSQL | @TransSQL(selectSql = "select xx from xx where id=#{变量名}") 说明:配置动态SQL,查询字典表、关联表进行翻译,支持Entity对象 |
@TransClass | @TransClass(catalog="类型code", service =XxxTransService.class) 说明:用户自定义翻译代码,如访问http接口进行翻译 |
3. 使用介绍
引入maven 依赖方式:将 jar 放在/libs目录下,配置如下
<dependency>
<groupId>com.geline.cloud</groupId>
<artifactId>coder-sdk-easy-trans</artifactId>
<scope>system</scope>
<version>1.3.2</version>
<systemPath>${project.basedir}/libs/coder-sdk-easy-trans-1.3.2.jar</systemPath>
</dependency>
自定义 TransDictHandler 实现类
/**
* 自定义翻译: 优先使用枚举翻译,找不到再使用表字典翻译
*/
@Component
public class MyTransDictHandler implements TransDictHandler {
@Resource
private DictEnumRegistry dictEnumRegistry;
@Resource
private SysDictDataMapper sysDictDataMapper;
@Override
public String getDictLabel(String dictType, Object dictValue) {
String dictLabel = this.dictEnumRegistry.getDictLabel(dictType, dictValue);
if(dictLabel != null) {
return dictLabel;
}
return sysDictDataMapper.selectDictLabel(dictType, String.valueOf(dictValue));
}
@Override
public Object getDictValue(String dictType, String dictLabel) {
Object dictValue = this.dictEnumRegistry.getDictValue(dictType, dictLabel);
if(dictValue != null) {
return dictValue;
}
return sysDictDataMapper.selectDictValue(dictType, dictLabel);
}
}
/**
* 字典翻译查询
*/
@Mapper
public interface SysDictDataMapper extends BaseMapper<SysDictData> {
//根据字典类型和字典值查询字典标签
@Select("select dict_label from sys_dict_data where dict_type = #{dictType} and dict_value = #{dictValue}")
String selectDictLabel(String dictType, String dictValue);
//根据字典类型和字典标签查询字典值
@Select("select dict_value from sys_dict_data where dict_type = #{dictType} and dict_label = #{dictLabel}")
String selectDictValue(String dictType, String dictLabel);
}
@DictType:自定义一个枚举字典类
import com.geline.easytrans.annotation.DictType;
import com.geline.easytrans.dict.IDict;
import lombok.AllArgsConstructor;
import lombok.Getter;
@DictType(value = "sys_is_used", label = "使用状态")
@AllArgsConstructor
@Getter
public enum IsUsedEnum implements IDict {
Y("Y", "正常"),
N("N", "未使用"),
D("D", "已删除");
private String code;
private String desc;
@Override
public String getLabel() {
return desc;
}
@Override
public Object getValue() {
return code;
}
}
@TransEnum:使用枚举字典翻译
@Getter
@Setter
public class ProjectDTO implements Serializable {
/* 应用状态:字典值 */
private String status;
/* 应用状态:字典名称 */
@TransEnum(dictType = "project_status", orgFieldName = "status")
private String statusDesc;
/* 业务类型 sys_oper_type */
private String operType;
@TransDict(dictType = "sys_oper_type", source = "operType")
private String operTypeDesc;
}
//定义一个枚举
@DictType(value = "project_status", label = "应用状态")
public enum ProjectStatusEnum implements IDict {
no("未开始"),
go("开发中"),
check("验收中"),
over("完成");
private String label;
ProjectStatusEnum(String label){
this.label = label;
}
@Override
public String getLabel() {
return label;
}
@Override
public Object getValue() {
return name();
}
}
@TransSQL:支持sql翻译,参数selectSql可以引用动态变量 #{userName}, ${userId}
@Getter
@Setter
public class ProjectDTO implements Serializable {
/* 用户ID */
private String userId;
/* 用户名,从表sys_user中查询用户名后回填*/
@TransSQL(selectSql = "select user_name from sys_user where id=#{userId}")
private String userName;
/* 应用状态值:字典值 project_status */
private String status;
/* 应用状态名:从表sys_dict_data中查询数据回填 */
@TransSQL(selectSql = "select dict_label from sys_dict_data where dict_type='project_status' and dict_value=#{status}")
private String statusDesc;
}
@TransClass:支持自定义ITransService 实现类进行翻译
@Getter
@Setter
public class ProjectDTO implements Serializable {
//获取当前租户ID
@TransClass(catalog="", service = TenantTransService.class)
private String tenantId;
}
@Component
public class TenantTransService implements TransClassService {
@Autowired
private BaseSessionService sessionService;
@Override
public void handField(String catalog, Object object, Field field, Map<String, Object> example) {
String fieldName = field.getName();
if("tenantId".equals(fieldName)){
String tenantId = sessionService.getTenantId();
BeanMap.create(object).put(fieldName, tenantId);
}
}
}
翻译方式一:手动调用
@RestController
@RequestMapping("/project")
@Slf4j
public class ProjectController {
@Autowired
private ProjectService projectService;
@Autowired
private EasyTransService transService;
/**
* Get localhost:9501/project/listByGroup/{groupId}
* @param groupId
* @return
*/
@ApiOperation(value = "查询应用列表(分组下)")
@GetMapping("/listByGroup/{groupId}")
public Result listByGroup(@PathVariable String groupId) {
List<ProjectDTO> list = this.projectService.listByGroup(groupId);
//翻译属性
transService.transBatch(list);
return Result.ok(list);
}
}
翻译方式二:全局自动翻译
/**
* 返回结果处理
* 处理字典翻译 @TransEnum, @TransSQL, @TransClass
*/
@RestControllerAdvice
@Slf4j
public class ResultBodyAdvice implements ResponseBodyAdvice {
@Autowired
protected EasyTransService easyTransService;
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
Class<?> parameterType = returnType.getParameterType();
return parameterType == Result.class || parameterType == TableDataInfo.class;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if(body instanceof Result){
Result bean = (Result)body;
if(bean.getCode()==0){
Object data = bean.getData();
if(data!=null){
if(data instanceof List){
easyTransService.transBatch((List) data);
}else {
easyTransService.transOne(data);
}
}
}
}else if(body instanceof TableDataInfo){
TableDataInfo bean = (TableDataInfo)body;
if(bean.getCode()==0){
List<?> rows = bean.getRows();
if(rows!=null){
easyTransService.transBatch(rows);
}
}
}
return body;
}
}
已经自定义了一些常用的枚举字典
源代码地址:https://gitee.com/cloud-coder/coder-sdk-easy-trans