说明:做项目中遇到一种场景,需要根据查询时间段, 获取时间段中中每个月份对应的金额(费用统计)。如截图中的两列
因为列是动态的, 首先想到的就是后天拼接JSON格式字符串, 然后返回到前台, 组装表头及内容。 但是当前系统中easyUI版本为1.2,并不支持 data属性(官方从1.3.2开始支持)。所以只能返回list<T> 格式。
网上一搜相关代码很多, 看客可以自己搜索一下。 我这里记录一下我当时使用场景及用法,已备以后使用。
1.需要引用cglib jar包, 我用的版本是2.2
2.建一个实体对象 DynamicBean.java 。主要用来处理对象。
- 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. 原来对象, 及需要拼接到对象中的属性字段集合处理方法。
- /**
- *参数说明:
- * 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;
- }
做笔记使用, 说不定以后还会用到。