自定义注解,实体属性查询具体需要的值

通过实体类上注解,标明需要查询的表名,字段,属性,值。进行动态sql查询赋值。

    @Autowired
    private ISysBaseAPI commonAPI;

    public void parseDictText(T record) {
            ObjectMapper mapper = new ObjectMapper();
            String json="{}";
            try {
                //解决@JsonFormat注解解析不了的问题详见SysAnnouncement类的@JsonFormat
                json = mapper.writeValueAsString(record);
            } catch (JsonProcessingException e) {
                log.error("json解析失败"+e.getMessage(),e);
            }
            JSONObject item = JSONObject.parseObject(json);
            //update-begin--Author:scott -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
            //for (Field field : record.getClass().getDeclaredFields()) {
            for (Field field : oConvertUtils.getAllFields(record)) {
                //update-end--Author:scott  -- Date:20190603 ----for:解决继承实体字段无法翻译问题------
                if (field.getAnnotation(Dict.class) != null) {
                    String code = field.getAnnotation(Dict.class).dicCode();
                    String text = field.getAnnotation(Dict.class).dicText();
                    String table = field.getAnnotation(Dict.class).dictTable();
                    String key = String.valueOf(item.get(field.getName()));

                    //翻译字典值对应的txt
                    String textValue = translateDictValue(code, text, table, key);
                    try {
                        field.setAccessible(true);
                        field.set(record,textValue);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
        }
    }

    /**
     *  翻译字典文本
     * @param code
     * @param text
     * @param table
     * @param key
     * @return
     */
    public String translateDictValue(String code, String text, String table, String key) {
        if(oConvertUtils.isEmpty(key)) {
            return null;
        }
        StringBuffer textValue=new StringBuffer();
        String[] keys = key.split(",");
        for (String k : keys) {
            String tmpValue = null;
            log.debug(" 字典 key : "+ k);
            if (k.trim().length() == 0) {
                continue; //跳过循环
            }
            if (!StringUtils.isEmpty(table)){
                log.debug("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
                tmpValue= commonAPI.translateDictFromTable(table,text,code,k.trim());
            }else {
                tmpValue = commonAPI.translateDict(code, k.trim());
            }
            if (tmpValue != null) {
                if (!"".equals(textValue.toString())) {
                    textValue.append(",");
                }
                textValue.append(tmpValue);
            }

        }
        return textValue.toString();
    }

接口:

/**
 * 6字典表的 翻译
 * @param table
 * @param text
 * @param code
 * @param key
 * @return
 */
String translateDictFromTable(String table, String text, String code, String key);

实现类:

@Override
public String translateDictFromTable(String table, String text, String code, String key) {
	return sysDictService.queryTableDictTextByKey(table, text, code, key);
}

mapper类:

	@Deprecated
	public String queryTableDictTextByKey(@Param("table") String table,@Param("text") String text,@Param("code") String code,@Param("key") String key);

xml:

<!--通过查询指定table的 text code key 获取字典值-->
<select id="queryTableDictTextByKey" parameterType="String" resultType="String">
	   select ${text} as "text" from ${table} where ${code}= #{key}
</select>

自定义注解:

/**
 *  类描述:  字典注解
 * 作    者: luoyan
 * 日    期: 2021年03月17日-下午9:37:16
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dict {
    /**
     * 方法描述:  数据code
     * 作    者: luoyan
     * 日    期: 2021年03月17日-下午9:37:16
     *
     * @return 返回类型: String
     */
    String dicCode();

    /**
     * 方法描述:  数据Text
     * 作    者: luoyan
     * 日    期: 2021年03月17日-下午9:37:16
     *
     * @return 返回类型: String
     */
    String dicText() default "";

    /**
     * 方法描述: 数据字典表
     * 作    者: luoyan
     * 日    期: 2021年03月17日-下午9:37:16
     *
     * @return 返回类型: String
     */
    String dictTable() default "";
}

实体类:使用@Dict注解通过指定的表,字段,进行数据查询到需要的值。

public class InspectionFiling implements Serializable {
    private static final long serialVersionUID = 1L;

	/**主键*/
	@TableId(type = IdType.ASSIGN_ID)
    @ApiModelProperty(value = "主键")
    private String id;
	
	/**定档规则*/
	@Excel(name = "定档规则", width = 30, dictTable = "gear_proportion", dicText = "proportion_name", dicCode = "id")
	@Dict(dictTable = "gear_proportion", dicText = "proportion_name", dicCode = "id")
    @ApiModelProperty(value = "定档规则")
    private String gradeRules;
	
    /**法院标识*/
    @ApiModelProperty(value = "法院标识")
    @Dict(dictTable ="basic_court_info",dicText = "orgname",dicCode = "id")
    private String caseCode;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值