JavaBean和Map 之间的相互转换

代码仅供参考:

/**
	 * 将一个 JavaBean 对象转化为一个 Map
	 * 
	 * @param bean
	 *            要转化的JavaBean 对象
	 * @param qualifier
	 *            hbase列族
	 * @return 转化出来的 Map 对象
	 * @throws IntrospectionException
	 *             如果分析类属性失败
	 * @throws IllegalAccessException
	 *             如果实例化 JavaBean 失败
	 * @throws InvocationTargetException
	 *             如果调用属性的 setter 方法失败
	 */
	public Map convertBean2Map(String qualifier, C bean)
			throws IntrospectionException, IllegalAccessException, InvocationTargetException {

		Class type = bean.getClass();
		Map returnMap = new HashMap();
		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")) {
				Method readMethod = descriptor.getReadMethod();
				Object result = readMethod.invoke(bean, new Object[0]);
				if (!StringUtils.isBlank(qualifier)) {
					returnMap.put(qualifier + ":" + propertyName, result);
				} else {
					returnMap.put(propertyName, result);
				}
				/*
				 * * if (result != null) {
				 * returnMap.put(qualifier+":"+propertyName, result); } else {
				 * returnMap.put(qualifier+":"+propertyName, ""); }
				 */
			}
		}
		return returnMap;
	}

 

 

 

//Map 转成 javaBean


public static Object convertMap2Bean(Class type, Map map)
			throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
		BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
		Object obj = type.newInstance(); // 创建 JavaBean 对象

		// 给 JavaBean 对象的属性赋值
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (int i = 0; i < propertyDescriptors.length; i++) {
			PropertyDescriptor descriptor = propertyDescriptors[i];
			String propertyName = descriptor.getName();

			String propertyType = descriptor.getPropertyType().toString();

			if (map.containsKey(propertyName)) {

				// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
				Object value = null;
				String key = propertyName;
				String valueStr = map.get(key);
				if (StringUtil.isNull(valueStr)) {
					continue;
				}
				// 转化原生类型
				if (propertyType.equals("int")) {
					value = Integer.parseInt(valueStr);
				} else if (propertyType.equals("boolean")) {
					value = Boolean.parseBoolean(valueStr);
				} else if (propertyType.equals("float")) {
					value = Float.parseFloat(valueStr);
				} else if (propertyType.equals("double")) {
					value = Double.parseDouble(valueStr);
				} else if (propertyType.equals("long")) {
					value = Long.parseLong(map.get(key));
				} else if (propertyType.equals("class java.lang.Integer")) {
					value = Integer.parseInt(valueStr);
				} else if (propertyType.equals("class java.lang.Long")) {
					value = new Long(valueStr);
				} else if (propertyType.equals("interface java.util.List")) {
					continue;
				} else if (propertyType.equals("class java.lang.String")) {
					value = map.get(key);
				} else if (propertyType.equals("interface java.util.Map")) {
					value = valueStr;
					// Map data = new HashMap();
					value = JSONObject.fromObject(value);
				} else {
					value = map.get(propertyName);
				}

				Object[] args = new Object[1];
				args[0] = value;

				try {
					descriptor.getWriteMethod().invoke(obj, args);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
		return obj;
	}

 

 

你可能感兴趣的:(------【Java】,●编程语言)