package com.nange.demo;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import com.nange.User;
/**
* java Bean与Map互转
* 作者: 一天
* 时间:2018-04-08 17:33:50
*
*/
public class MapBeanConvertUtils {
/**
* 功能:Map --> Bean 2: 利用org.apache.commons.beanutils 工具类实现 Map --> Bean
* 作者: 一天
* 时间:2018年4月8日 下午5:34:23
* @param map
* @param obj
*/
public static void Map2Bean(Map<String, Object> map, Object obj) {
if (map == null || obj == null) {
return;
}
try {
BeanUtils.populate(obj, map);
} catch (Exception e) {
System.out.println("transMap2Bean2 Error " + e);
}
}
/**
* Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
* userName -->userName
* 作者: 一天
* 时间:2018年4月8日 下午5:34:42
* @param map
* @param obj
*/
public static void Map2Bean2(Map<String, Object> map, Object obj) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
System.out.println("transMap2Bean Error " + e);
}
return;
}
/**
* 功能: Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
* 固定hibernateBean类型转换: USER_NAME 转 userName ,注意严格验证参数
* 作者: 一天
* 时间:2018年4月8日 下午5:35:04
* @param map
* @param obj
*/
public static void Map2BeanBig(Map<String, Object> map, Object obj) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
//将key中大写字符转为_大写
key = underscoreNameBig(key);
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
System.out.println("transMap2Bean Error " + e);
}
return;
}
/**
* 功能: Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
* 固定hibernateBean类型转换: user_name 转 userName ,注意严格验证参数
* 作者: 一天
* 时间:2018年4月8日 下午5:35:50
* @param map
* @param obj
*/
public static void Map2BeanLittle(Map<String, Object> map, Object obj) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
//将key中大写字符转为_大写
key = underscoreNameLittle(key);
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
System.out.println("transMap2Bean Error " + e);
}
return;
}
/**
* Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
* 作者:一天
* 时间:2017年11月10日 下午12:06:46
* @param obj
* @return
*/
public static Map<String, Object> Bean2Map(Object obj) {
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
/**
* 功能:将大写转为下划线,且大写 如: userName > user_name
* @author wuxw
* @param name
* @return
* @throws
*/
public static String underscoreNameLittle(String name)
{
StringBuilder result = new StringBuilder();
if ((name != null) && (name.length() > 0)) {
result.append(name.substring(0, 1).toLowerCase());
for (int i = 1; i < name.length(); ++i) {
String s = name.substring(i, i + 1);
if (s.equals(s.toUpperCase())) {
result.append("_");
result.append(s.toLowerCase());
}
else {
result.append(s);
}
}
}
return result.toString();
}
/**
* 功能:将大写转为下划线,且大写 如: userName > USER_NAME
* @author wuxw
* @param name
* @return
* @throws
*/
public static String underscoreNameBig(String name)
{
StringBuilder result = new StringBuilder();
if ((name != null) && (name.length() > 0)) {
result.append(name.substring(0, 1).toLowerCase());
for (int i = 1; i < name.length(); ++i) {
String s = name.substring(i, i + 1);
if (s.equals(s.toUpperCase())) {
result.append("_");
result.append(s);
}
else {
result.append(s);
}
}
}
return result.toString().toUpperCase();
}
public static void main(String[] args) {
User user=new User(10,"aQ");
Map<String, Object> map = Bean2Map(user);
System.out.println(map);
}
}
java Bean与Map相互转
最新推荐文章于 2023-09-05 22:35:09 发布