日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 数据字典 spring_springboot+redis+切面实现数据字典功能

發布時間:2024/8/23 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 数据字典 spring_springboot+redis+切面实现数据字典功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義注解:DataDict,用于bo對象類,需要翻譯的屬性

package com.zddts.common.annotation.dict;

import java.lang.annotation.*;

/**

* 說明:數據字典處理類

* Created by luojie on 2019/05/29.

*/

//@DataDict( dict="patType", source = "patType" )

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface DataDict {

/**

* 方法描述 描述標準編碼

* @return

*/

String dict() default "";

/**

* 方法描述,可使用占位符獲取參數:{{source}}

* 主要標準編碼之來源

*/

String source() default "";

}

自定義注解:

DataDictClass 用來表面返回對象集合需要,本功能目前只支持bean對象的屬性翻譯

package com.zddts.common.annotation.dict;

import java.lang.annotation.*;

/**

* 說明:

* 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;

/**

* 說明:數據字典切面類

* Created by luojie on 2019/05/29.

*/

@Aspect

@Component

public class DataDictAspect {

@Autowired

PubappClient pubappClient;

/**

* 非基本類型在 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 后期需優化讀取Redis

List dictInfos = pubappClient.getPuCodeByType("patType");

if (dictInfos == null && dictInfos.size() == 0) {

return result;

}

//先把字典值轉成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;

}

/**

* 單個設置值

*

* @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);

//設置值

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;

}

}

使用:

對要數據字典轉換的方法加上DataDictClass注解

需要注解翻譯的加上注解DataDict ,dict是指標準碼的編碼

工具類:主要用了反射機制

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 {

/**

* 方法說明:將List轉換為List

*

* @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;

}

/**

* 設置bean 屬性值,沒有下劃線的

*

* @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;

}

/**

* 設置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;

}

/**

* 方法說明:將List轉換為List

*

* @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;

}

/**

* 設置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);

// 判斷字段的無參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;

}

/**

* 方法說明:獲取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;

}

/**

* 方法說明:獲取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;

}

/**

* 設置對應值

*

* @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;

}

//為空不設置

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()));

}

}

}

/**

* 設置對應值

*

* @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);

// 判斷字段的無參get方法是否存在

Method method = getBeanMethod(cls, getMethodName, new Class[]{});

Object fieldValue = method.invoke(obj, new Object[]{});

return fieldValue;

}

}

source 指bean中翻譯所需要對應的值字段

---------------------

總結

以上是生活随笔為你收集整理的java 数据字典 spring_springboot+redis+切面实现数据字典功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。