springboot之数据翻译组件:支持枚举、字典表、字段、对象、方法翻译

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;


    @ApiModelProperty(value = "部门名称(多个,隔开)")
    @TransSQL(selectSql = "select group_concat(d.dept_name) from sys_dept d left join sys_user2dept u2d on u2d.dept_id=d.dept_id where u2d.user_id=#{userId} group by u2d.user_id")
    private String deptName;

    @ApiModelProperty(value = "密级 (字典:secret_type)")
    @Length(max = 30, message="secretLevel长度必须<=30")
    private String secretLevel;

    @ApiModelProperty(value = "年限")
    private String secretYear;

    @ApiModelProperty(value = "密级描述(举例:内部 * 3 年)")
    @TransSQL(selectSql = "select concat(dict_label, ' * ${secretYear}年') from sys_dict_data where dict_type='secret_type' and dict_value=#{secretLevel}")
    private String secretLevelDesc;
}

@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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星梦天河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值