使用aop实现注解式数据字典

本文介绍了如何使用AOP和自定义注解`Dict`来实现数据字典的注解式处理。在`DictAspect`切面类中拦截controller方法,通过`oConvertUtils`工具类对需要翻译的字段进行转换。查询结果会封装在`PageUtils`类中,方便后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dict

这个是我们的注解类,把它打在我们要翻译的实体类上就ojbk了

/**
 *  类描述:  字典注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dict {

    /**
     * 方法描述:  数据code
     * @return 返回类型: String
     */
    String dicCode();

    /**
     * 方法描述:  这是返回后Put到josn中的文本 key 值
     * @return 返回类型: String
     */
    String dicText() default "";
}

DictAspect 注解切面类

这个类核心思想就是拦截所有的 controller 方法,对要翻译的字段进行处理

package com.p2pInternetloan.sys.aspect;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.p2pInternetloan.base.aspect.annotation.Dict;
import com.p2pInternetloan.base.constant.CommonConstant;
import com.p2pInternetloan.base.utils.PageUtils;
import com.p2pInternetloan.base.utils.oConvertUtils;
import com.p2pInternetloan.sys.service.SysdictitemService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 字典aop类 (用于翻译字典数据)
 * @Author: cpc
 * @Date: 2019-3-17 21:50
 * @Version: 1.0
 */
@Aspect
@Component
@Slf4j
public class DictAspect {
	//这是操作数据字典那张表的 service 
    @Autowired
    private SysdictitemService sysdictitemService;

    // 定义切点Pointcut 拦截所有对服务器的请求
    @Pointcut("execution( * com.p2pInternetloan..controller.*.*(..))")
    public void excudeService() {
    }

    /**
     * 这是触发 excudeService 的时候会执行的
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("excudeService()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        //这是定义开始事件
    	long time1= System.currentTimeMillis();
    	//这是方法并获取返回结果
        Object result = pjp.proceed();
        //这是获取到 结束时间
        long time2= System.currentTimeMillis();
        log.debug("获取JSON数据 耗时:"+(time2-time1)+"ms");
        //解析开始时间
        long start= System.currentTimeMillis();
        //开始解析(翻译字段内部的值凡是打了 @Dict 这玩意的都会被翻译)
        this.parseDictText(result);
        //解析结束时间
        long end= System.currentTimeMillis();
        log.debug("解析注入JSON数据  耗时"+(end-start)+"ms");
        return result;
    }

    /**
     * 本方法针对返回对象为Result 的IPage的分页列表数据进行动态字典注入
     * 字典注入实现 通过对实体类添加注解@dict 来标识需要的字典内容,字典分为单字典code即可 ,table字典 code table text配合使用与原来jeecg的用法相同
     * 示例为SysUser   字段为sex 添加了注解@Dict(dicCode = "sex") 会在字典服务立马查出来对应的text 然后在请求list的时候将这个字典text,已字段名称加_dictText形式返回到前端
     * 例输入当前返回值的就会多出一个sex_dictText字段
     * {
     *      sex:1,
     *      sex_dictText:"男"
     * }
     * 前端直接取值sext_dictText在table里面无需再进行前端的字典转换了
     *  customRender:function (text) {
     *               if(text==1){
     *                 return "男";
     *               }else if(text==2){
     *                 return "女";
     *               }else{
     *                 return text;
     *               }
     *             }
     *             目前vue是这么进行字典渲染到table上的多了就很麻烦了 这个直接在服务端渲染完成前端可以直接用
     * @param result
     */
    private void parseDictText(Object result) {
        if (result instanceof PageUtils) {
                List<JSONObject> items = new ArrayList<>();
                PageUtils pageUtils = (PageUtils)result;
                //循环查找出来的数据
                for (Object record :  pageUtils.getData()) {
                    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 key = String.valueOf(item.get(field.getName()));
                            //翻译字典值对应的txt
                            String textValue = translateDictValue(code, key);
                            //  CommonConstant.DICT_TEXT_SUFFIX的值为,是默认值:
                            // public static final String DICT_TEXT_SUFFIX = "_dictText";
                            log.debug(" 字典Val : "+ textValue);
                            log.debug(" __翻译字典字段__ "+field.getName() + CommonConstant.DICT_TEXT_SUFFIX+": "+ textValue);
                            //如果给了文本名
                            if(!StringUtils.isEmpty(text)){
                                item.put(text, textValue);
                            }else{
                                //走默认策略
                                item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
                            }

                        }
                        //date类型默认转换string格式化日期
                        if (field.getType().getName().equals("java.util.Date")&&field.getAnnotation(JsonFormat.class)==null&&item.get(field.getName())!=null){
                            SimpleDateFormat aDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            item.put(field.getName(), aDate.format(new Date((Long) item.get(field.getName()))));
                        }
                    }
                    items.add(item);
                }
                 pageUtils.setData(items);

            }
    }


    /**
          *  翻译字典文本
     * @param code
     * @param key
     * @return
     */
    private String translateDictValue(String code, String key) {
        //如果key为空直接返回就好了
    	if(oConvertUtils.isEmpty(key)) {
    		return null;
    	}
        StringBuffer textValue=new StringBuffer();
    	//分割 key 值
        String[] keys = key.split(",");
        //循环 keys 中的所有值
        for (String k : keys) {
            String tmpValue = null;
            log.debug(" 字典 key : "+ k);
            if (k.trim().length() == 0) {
                continue; //跳过循环
            }
            tmpValue = sysdictitemService.queryDictTextByKey(code, k.trim());

            if (tmpValue != null) {
                if (!"".equals(textValue.toString())) {
                    textValue.append(",");
                }
                textValue.append(tmpValue);
            }
        }
        //返回翻译的值
        return textValue.toString();
    }

}

oConvertUtils

    /**
     * 获取类的所有属性,包括父类
     *
     * @param object
     * @return
     */
    public static Field[] getAllFields(Object object) {
        Class<?> clazz = object.getClass();
        List<Field> fieldList = new ArrayList<>();
        while (clazz != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }
    
   public static boolean isEmpty(Object object) {
        if (object == null) {
                return (true);
            }
            if ("".equals(object)) {
                return (true);
            }
            if ("null".equals(object)) {
                return (true);
            }
            return (false);
        }

PageUtils 这是查询结果集合封装类

package com.p2pInternetloan.base.utils;

import java.io.Serializable;
import java.util.List;

/**
 * 分页查询返回结果封装
 */
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	//这是总行数
	private long total;
	//这是保持查询出来的数据
	private List<?> data;

	/**
	 *
	 * @param list 保存数据
	 * @param total 查到多行数据
	 */
	public PageUtils(List<?> list, long total) {
		this.data = list;
		this.total = total;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }

    public static long getSerialVersionUID() {
		return serialVersionUID;
	}
}

使用

在查询的实体类上加如下注解:
在这里插入图片描述
下面是查询的方法:
在这里插入图片描述
查询结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值