java 数据字典 spring_springboot+redis+切面实现数据字典功能
自定義注解:DataDict,用于bo對(duì)象類(lèi),需要翻譯的屬性
package com.zddts.common.annotation.dict;
import java.lang.annotation.*;
/**
* 說(shuō)明:數(shù)據(jù)字典處理類(lèi)
* Created by luojie on 2019/05/29.
*/
//@DataDict( dict="patType", source = "patType" )
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataDict {
/**
* 方法描述 描述標(biāo)準(zhǔn)編碼
* @return
*/
String dict() default "";
/**
* 方法描述,可使用占位符獲取參數(shù):{{source}}
* 主要標(biāo)準(zhǔn)編碼之來(lái)源
*/
String source() default "";
}
自定義注解:
DataDictClass 用來(lái)表面返回對(duì)象集合需要,本功能目前只支持bean對(duì)象的屬性翻譯
package com.zddts.common.annotation.dict;
import java.lang.annotation.*;
/**
* 說(shuō)明:
* Created by luojie on 2019/05/29.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataDictClass {
}
切面處理:?DataDictAspect
package com.zddts.ac.aop;
import com.alibaba.fastjson.JSON;
import com.zddts.ac.client.PubappClient;
import com.zddts.common.annotation.dict.DataDict;
import com.zddts.common.annotation.dict.DataDictClass;
import com.zddts.common.bo.pubapp.PuCodeBo;
import com.zddts.common.utils.BeanUtils;
import org.apache.commons.lang.StringUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 說(shuō)明:數(shù)據(jù)字典切面類(lèi)
* Created by luojie on 2019/05/29.
*/
@Aspect
@Component
public class DataDictAspect {
@Autowired
PubappClient pubappClient;
/**
* 非基本類(lèi)型在 CLASS 中的定義
*/
private static final String FILED_NAME_TYPE = "TYPE";
private Map dictInfoMap = new ConcurrentHashMap<>();
@Pointcut("@annotation(dataDictClass)")
public void doDataDictClass(DataDictClass dataDictClass) {
}
@Around("@annotation(dataDictClass)")
public Object translation(final ProceedingJoinPoint pjp, DataDictClass dataDictClass) throws Throwable {
Object result = pjp.proceed();
if (result == null) {
return result;
}
Object obj;
if (result instanceof List || result instanceof ArrayList) {
List olist = ((List) result);
if (olist.size() == 0) {
return result;
}
obj = olist.get(0);
} else {
obj = result;
}
List> dictParams = boDict(obj.getClass());
if (dictParams.size() == 0) {
return result;
}
//TODO 后期需優(yōu)化讀取Redis
List dictInfos = pubappClient.getPuCodeByType("patType");
if (dictInfos == null && dictInfos.size() == 0) {
return result;
}
//先把字典值轉(zhuǎn)成map
for (PuCodeBo puCodeBo : dictInfos) {
dictInfoMap.put(puCodeBo.getCodeType() + puCodeBo.getValue(), puCodeBo.getCodeName());
}
if (result instanceof List || result instanceof ArrayList) {
for (Object o : (List) result) {
sign(o, dictParams, dictInfoMap);
}
} else {
sign(result, dictParams, dictInfoMap);
}
return result;
}
/**
* 單個(gè)設(shè)置值
*
* @param obj
* @param dictParams
* @param dictInfoMap
*/
public void sign(Object obj, List> dictParams, Map dictInfoMap) {
for (Map dictParam : dictParams) {
String dict = dictParam.get("dict");
String source = dictParam.get("source");
String dictName = dictParam.get("dictName");
try {
//獲取源編碼值
String sourceValue = (String) BeanUtils.getBeanFieldValue(obj.getClass(), obj, source);
String dictCodeName = dictInfoMap.get(dict + sourceValue);
//設(shè)置值
BeanUtils.setBeanField(obj.getClass(), obj, dictName, dictCodeName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 獲取bo中屬性值
*
* @param cla
* @return
*/
private List> boDict(Class cla) {
Field[] fields = cla.getDeclaredFields();
List> list = new ArrayList>();
Map map;
DataDict dataDict;
for (Field field : fields) {
if (field.isAnnotationPresent(DataDict.class)) {
map = new HashMap();
dataDict = field.getAnnotation(DataDict.class);
map.put("dict", dataDict.dict());
map.put("source", dataDict.source());
map.put("dictName", field.getName());
list.add(map);
}
}
return list;
}
}
使用:
對(duì)要數(shù)據(jù)字典轉(zhuǎn)換的方法加上DataDictClass注解
需要注解翻譯的加上注解DataDict ,dict是指標(biāo)準(zhǔn)碼的編碼
工具類(lèi):主要用了反射機(jī)制
package com.zddts.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
public class BeanUtils {
/**
* 方法說(shuō)明:將List轉(zhuǎn)換為L(zhǎng)ist
*
* @param mapList
* @param cls
* @return
* @throws Exception
*/
public static List mapListToBeanList(
List> mapList, Class> cls) throws Exception {
if (mapList == null || mapList.size() == 0) {
return null;
}
List beanList = new ArrayList();
Object bean = null;
for (Map map : mapList) {
bean = mapToBean(map, cls);
if (bean == null) {
continue;
}
beanList.add(bean);
}
return beanList;
}
/**
* 設(shè)置bean 屬性值,沒(méi)有下劃線(xiàn)的
*
* @param map
* @param cls
* @return
* @throws Exception
*/
public static Object mapToBeanNL(Map map, Class> cls) throws Exception {
if (map == null || map.size() == 0) {
return null;
}
Object obj = cls.newInstance();
for (Entry entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
continue;
}
// 判斷字段是否存在
String fieldName = key;
Field field = getBeanField(cls, fieldName);
if (field == null) {
continue;
}
// 判斷字段的set方法是否存在
String setMethodName = StringUtils.pareSetName(fieldName);
Method method = getBeanMethod(cls, setMethodName, field.getType());
if (method == null) {
continue;
}
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("long".equals(fieldType) || "Long".equals(fieldType)) {
method.invoke(obj, Long.valueOf(value.toString()));
} else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
method.invoke(obj, Double.valueOf(value.toString()));
} else if ("float".equals(fieldType) || "Float".equals(fieldType)) {
method.invoke(obj, Float.valueOf(value.toString()));
} else if ("boolean".equals(fieldType) || "Boolean".equals(fieldType)) {
if (value.getClass().equals(Boolean.class)) {
method.invoke(obj, (Boolean) value);
} else {
method.invoke(obj, Boolean.valueOf(value.toString()));
}
} else if ("Date".equals(fieldType)) {
if (value != null) {
if (value.getClass().equals(Date.class)) {
method.invoke(obj, (Date) value);
} else {
method.invoke(obj, DateUtils.strToDate(value.toString()));
}
}
}
}
return obj;
}
/**
* 設(shè)置bean 屬性值
*
* @param map
* @param cls
* @return
* @throws Exception
*/
public static Object mapToBean(Map map, Class> cls)
throws Exception {
if (map == null || map.size() == 0) {
return null;
}
Object obj = cls.newInstance();
for (Entry entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
continue;
}
// 判斷字段是否存在
String fieldName = StringUtils.toUnderLine(key.toLowerCase());
Field field = getBeanField(cls, fieldName);
if (field == null) {
continue;
}
// 判斷字段的set方法是否存在
String setMethodName = StringUtils.pareSetName(fieldName);
Method method = getBeanMethod(cls, setMethodName, field.getType());
if (method == null) {
continue;
}
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("long".equals(fieldType) || "Long".equals(fieldType)) {
method.invoke(obj, Long.valueOf(value.toString()));
} else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
method.invoke(obj, Double.valueOf(value.toString()));
} else if ("float".equals(fieldType) || "Float".equals(fieldType)) {
method.invoke(obj, Float.valueOf(value.toString()));
} else if ("boolean".equals(fieldType)
|| "Boolean".equals(fieldType)) {
if (value.getClass().equals(Boolean.class)) {
method.invoke(obj, (Boolean) value);
} else {
method.invoke(obj, Boolean.valueOf(value.toString()));
}
} else if ("Date".equals(fieldType)) {
if (value != null) {
if (value.getClass().equals(Date.class)) {
method.invoke(obj, (Date) value);
} else {
method.invoke(obj, DateUtils
.strToDate(value.toString()));
}
}
}
}
return obj;
}
/**
* 方法說(shuō)明:將List轉(zhuǎn)換為L(zhǎng)ist
*
* @param beanList
* @return
* @throws Exception
*/
public static List> beanListToMapList(
List> beanList) throws Exception {
if (beanList == null || beanList.size() == 0) {
return null;
}
List> mapList = new ArrayList>();
Map map = null;
for (Object bean : beanList) {
map = beanToMap(bean);
if (map == null || map.size() == 0) {
continue;
}
mapList.add(map);
}
return mapList;
}
/**
* 設(shè)置bean 屬性值
*
* @param bean
* @return
* @throws Exception
*/
public static Map beanToMap(Object bean) throws Exception {
if (bean == null) {
return null;
}
Map map = new HashMap();
Class> cls = bean.getClass();
Field fields[] = cls.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
String fieldType = field.getType().getSimpleName();
boolean isBooleanType = false;
if (fieldType.equals("boolean") || fieldType.equals("Boolean")) {
isBooleanType = true;
}
String getMethodName = StringUtils.pareGetName(fieldName,
isBooleanType);
// 判斷字段的無(wú)參get方法是否存在
Method method = getBeanMethod(cls, getMethodName, new Class[]{});
if (method == null) {
continue;
}
Object fieldValue = method.invoke(bean, new Object[]{});
map.put(StringUtils.toUnderLine(field.getName()).toUpperCase(),
fieldValue);
}
return map;
}
/**
* 判斷該方法是否存在
*
* @param methods
* @param met
* @return
*/
public static boolean checkMethod(Method methods[], String met) {
if (null != methods) {
for (Method method : methods) {
if (met.equals(method.getName())) {
return true;
}
}
}
return false;
}
/**
* 方法說(shuō)明:獲取bean的指定方法
*
*
* Author: zhenqiangs Create Date: 2016-4-30 下午01:07:12 History: 2016-4-30
* 下午01:07:12 zhenqiangs Created.
*
* @param cls
* @param methodName
* @param paramTypes
* @return
*/
private static Method getBeanMethod(Class> cls, String methodName,
Class>... paramTypes) {
if (cls == null) {
return null;
}
Method setMethod = null;
try {
setMethod = cls.getMethod(methodName, paramTypes);
} catch (Exception e) {
}
return setMethod;
}
/**
* 方法說(shuō)明:獲取bean的指定屬性
*
* @param cls
* @param fieldName
* @return
*/
public static Field getBeanField(Class> cls, String fieldName) {
if (cls == null) {
return null;
}
Field field = null;
try {
field = cls.getDeclaredField(fieldName);
} catch (Exception e) {
}
return field;
}
/**
* 設(shè)置對(duì)應(yīng)值
*
* @param fieldName
*/
public static void setBeanField(Class> cls, Object obj, String fieldName, Object value) throws Exception {
// 判斷字段是否存在
Field field = getBeanField(cls, fieldName);
if (field == null) {
return;
}
// 判斷字段的set方法是否存在
String setMethodName = StringUtils.pareSetName(fieldName);
Method method = getBeanMethod(cls, setMethodName, field.getType());
if (method == null) {
return;
}
//為空不設(shè)置
if (value == null) {
return;
}
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("long".equals(fieldType) || "Long".equals(fieldType)) {
method.invoke(obj, Long.valueOf(value.toString()));
} else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
method.invoke(obj, value.toString());
} else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
method.invoke(obj, Double.valueOf(value.toString()));
} else if ("float".equals(fieldType) || "Float".equals(fieldType)) {
method.invoke(obj, Float.valueOf(value.toString()));
} else if ("boolean".equals(fieldType) || "Boolean".equals(fieldType)) {
if (value.getClass().equals(Boolean.class)) {
method.invoke(obj, (Boolean) value);
} else {
method.invoke(obj, Boolean.valueOf(value.toString()));
}
} else if ("Date".equals(fieldType)) {
if (value.getClass().equals(Date.class)) {
method.invoke(obj, (Date) value);
} else {
method.invoke(obj, DateUtils.strToDate(value.toString()));
}
}
}
/**
* 設(shè)置對(duì)應(yīng)值
*
* @param fieldName
*/
public static Object getBeanFieldValue(Class> cls, Object obj, String fieldName) throws Exception {
// 判斷字段是否存在
Field field = getBeanField(cls, fieldName);
// 判斷字段的set方法是否存在
String getMethodName = StringUtils.pareGetName(fieldName, false);
// 判斷字段的無(wú)參get方法是否存在
Method method = getBeanMethod(cls, getMethodName, new Class[]{});
Object fieldValue = method.invoke(obj, new Object[]{});
return fieldValue;
}
}
source 指bean中翻譯所需要對(duì)應(yīng)的值字段
---------------------
總結(jié)
以上是生活随笔為你收集整理的java 数据字典 spring_springboot+redis+切面实现数据字典功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 迷你世界无人驾驶地铁火车_出口伊斯坦布尔
- 下一篇: android评论嵌套,android