激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 類動態添加屬性字段的操作

Java 類動態添加屬性字段的操作

2021-08-09 10:58前塵憶夢 Java教程

這篇文章主要介紹了Java 類動態添加屬性字段的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

說明:

做項目中遇到一種場景,需要根據查詢時間段, 獲取時間段中中每個月份對應的金額(費用統計)。

如截圖中的兩列

Java 類動態添加屬性字段的操作

因為列是動態的, 首先想到的就是后天拼接JSON格式字符串, 然后返回到前臺, 組裝表頭及內容。

但是當前系統中easyUI版本為1.2,并不支持 data屬性(官方從1.3.2開始支持)。所以只能返回list<T> 格式。

網上一搜相關代碼很多, 看客可以自己搜索一下。 我這里記錄一下我當時使用場景及用法,已備以后使用。

1.需要引用cglib jar包, 我用的版本是2.2

2.建一個實體對象 DynamicBean.java 。主要用來處理對象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class DynamicBean {
 private Object object = null; // 動態生成的類
 private BeanMap beanMap = null; // 存放屬性名稱以及屬性的類型
 public DynamicBean() {
  super();
 }
 
 public DynamicBean(Map propertyMap) {
  this.object = generateBean(propertyMap);
  this.beanMap = BeanMap.create(this.object);
 }
 
 /**
  * @param propertyMap
  * @return
  */
 private Object generateBean(Map propertyMap) {
  BeanGenerator generator = new BeanGenerator();
  Set keySet = propertyMap.keySet();
  for (Iterator<String> i = keySet.iterator(); i.hasNext();) {
   String key = (String) i.next();
   generator.addProperty(key, (Class) propertyMap.get(key));
  }
  return generator.create();
 }
 
 /**
  * ??bean??????
  * @param property ??????
  * @param value ?
  */
 public void setValue(Object property, Object value) {
  beanMap.put(property, value);
 }
 
 /**
  * ??????????õ??????
  * @param property ??????
  * @return ?
  */
 public Object getValue(String property) {
  return beanMap.get(property);
 }
 
 /**
  * 返回新生成的對象
  * @return
  */
 public Object getObject() {
  return this.object;
 }
}

3. 原來對象, 及需要拼接到對象中的屬性字段集合處理方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
*參數說明:
* object : 查詢結果數組中對象。
* moneyMap : 為對象對應所有月份數據集合
* 解釋:已經查詢出一組賬單對象集合List<Bill> , 而moneyMap為對象中的一個屬性
* Map<String,Bigdecimal>, 存放了月份及金額
*/
private Object dynamicClass(Object object, Map<String, BigDecimal> moneyMap) throws Exception {
  // 字段 - 值 集合
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
  // 字段 - 字段類型 集合
  HashMap<String, Object> typeMap = new HashMap<String, Object>();
  // 獲取傳入類
  Class<? extends Object> type = object.getClass();
  BeanInfo beanInfo = Introspector.getBeanInfo(type);
  PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  // 獲取對象中已存在的數據
  for (int i = 0; i < propertyDescriptors.length; i++) {
   PropertyDescriptor descriptor = propertyDescriptors[i];
   String propertyName = descriptor.getName();
   if (!propertyName.equals("class") && !propertyName.equals("monthMap")) {
    Method readMethod = descriptor.getReadMethod();
    Object result = readMethod.invoke(object, new Object[0]);
    if (result != null) {
     returnMap.put(propertyName, result);
    } else {
     String propertyType = descriptor.getPropertyType().toString();
     if (propertyType.contains("java.math.BigDecimal")) {
      returnMap.put(propertyName, new BigDecimal(0));
     } else {
      returnMap.put(propertyName, "");
     }
    }
    typeMap.put(propertyName, descriptor.getPropertyType());
   }
  }
  // 獲取月份數據, 變為字段屬性
  Set<String> monthKeys = moneyMap.keySet();
  for (Iterator<String> it = monthKeys.iterator(); it.hasNext();) {
   String key = (String) it.next();
   // 字段類型
   typeMap.put(key, Class.forName("java.math.BigDecimal"));
   // 字段對應值
   returnMap.put(key, moneyMap.get(key));
  }
  // map轉換成實體對象
  DynamicBean bean = new DynamicBean(typeMap);
  // 賦值
  Set<String> keys = typeMap.keySet();
  for (Iterator<String> it = keys.iterator(); it.hasNext();) {
   String key = (String) it.next();
   bean.setValue(key, returnMap.get(key));
  }
  Object obj = bean.getObject();
  return obj;
 }

做筆記使用, 說不定以后還會用到。

補充:java動態的生成類的屬性、并賦值

1、springboot項目中,在build.gradle中,配置jar包

?
1
2
compile("commons-beanutils:commons-beanutils:1.9.3")
compile("cglib:cglib-nodep:3.2.4")

2、創建DynamicBean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Map;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBean {
 
 /**
  * 目標對象
  */
 private Object target;
 /**
  * 屬性集合
  */
 private BeanMap beanMap;
 
 public DynamicBean(Class superclass, Map<String, Class> propertyMap){
  this.target = generateBean(superclass, propertyMap);
  this.beanMap = BeanMap.create(this.target);
 }
 
 /**
  * bean 添加屬性和值
  *
  * @param property
  * @param value
  */
 public void setValue(String property, Object value) {
  beanMap.put(property, value);
 }
 
 /**
  * 獲取屬性值
  *
  * @param property
  * @return
  */
 public Object getValue(String property) {
  return beanMap.get(property);
 }
 
 /**
  * 獲取對象
  *
  * @return
  */
 public Object getTarget() {
  return this.target;
 }
 
 /**
  * 根據屬性生成對象
  *
  * @param superclass
  * @param propertyMap
  * @return
  */
 private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
  BeanGenerator generator = new BeanGenerator();
  if (null != superclass) {
   generator.setSuperclass(superclass);
  }
  BeanGenerator.addProperties(generator, propertyMap);
  return generator.create();
 }
}

3、創建ReflecUtil轉換的工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.commons.beanutils.PropertyUtilsBean;
import com.sunxung.factoring.entity.DynamicBean;
import com.sunxung.factoring.entity.attendanceManagement.AttendanceVo;
 
/**
 * @className:ReflectUtil
 * @description:動態生成類的屬性、并且賦值
 * @date:2018年4月3日 下午2:33:10
 */
public class ReflectUtil {
 
 static Logger logger = LogManager.getLogger(ReflectUtil.class);
 
 @SuppressWarnings("rawtypes")
 public static Object getTarget(Object dest, Map<String, Object> addProperties) {
  PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
  PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
  Map<String, Class> propertyMap = new HashMap<>();
  for (PropertyDescriptor d : descriptors) {
   if (!"class".equalsIgnoreCase(d.getName())) {
    propertyMap.put(d.getName(), d.getPropertyType());
   }
  }
  // add extra properties
  addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
  // new dynamic bean
  DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
  // add old value
  propertyMap.forEach((k, v) -> {
   try {
    // filter extra properties
    if (!addProperties.containsKey(k)) {
     dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
    }
   } catch (Exception e) {
    logger.error(e.getMessage(), e);
   }
  });
  // add extra value
  addProperties.forEach((k, v) -> {
   try {
    dynamicBean.setValue(k, v);
   } catch (Exception e) {
    logger.error(e.getMessage(), e);
   }
  });
  Object target = dynamicBean.getTarget();
  return target;
 }
 
 public static void main(String[] args) {
  AttendanceVo entity = new AttendanceVo();
  Map<String, Object> addProperties = new HashMap<>();
  addProperties.put("day31", "你好");
  AttendanceVo newVo = (AttendanceVo) getTarget(entity, addProperties);
  System.out.println(newVo.getDay0());
 }
}

4、在項目中動態生成屬性并且賦值的使用

?
1
2
3
4
5
6
7
8
9
10
11
private AttendanceVo autoDetailNew(SearchAttendanceVo search, AttendanceVo att) {
  search.setDingdingUserId(att.getDingdingUserId());
  List<Attendance> detailList = attendanceMapper.findDetailList(search);
  Map<String, Object> addProperties = new HashMap<>();
  for (Attendance attendance : detailList) {
   addProperties.put("day" + DateUtil.getDayStringFormatYMD(attendance.getAttendanceDate()),
     attendance.getRemark());
  }
  AttendanceVo newVo = (AttendanceVo) ReflectUtil.getTarget(att, addProperties);
  return newVo;
 }

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。

原文鏈接:https://blog.csdn.net/WUWENJINWUWENJIN/article/details/83276553

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品资源手机在线播放 | 一区二区三区欧洲 | 久久精精 | 成人超碰 | 国产精品啪一品二区三区粉嫩 | 国产精品久久久久国产精品三级 | 深夜小视频在线观看 | 91午夜免费视频 | 鲁一鲁一鲁一鲁一曰综合网 | 99精品欧美一区二区 | 国产精品视频1区 | 久久99国产综合精品 | 免费黄色小视频网站 | 最新一区二区三区 | 欧美 日韩 中文 | 久久最新免费视频 | 成人免费毛片一 | 性生活视频软件 | 中文字幕xxx | 一级片999| 宅男噜噜噜66国产免费观看 | 中文字幕在线免费看 | 七首小情歌泰剧在线播放 | 一区在线看| 国产69精品久久久久孕妇黑 | 欧美日韩成人一区二区 | 毛片在线视频在线播放 | 久久久久夜色精品国产老牛91 | 亚洲一区二区三区高清视频 | 亚洲综合视频网站 | 蜜桃av网 | 国产精选91| 一区二区久久久久草草 | 国产精品一区二区在线 | 欧美性视频一区二区 | 久久久久久艹 | 欧美高清在线精品一区二区不卡 | 欧美日韩亚洲另类 | 91看大片 | 久久久久九九九女人毛片 | 操操操操操 |