将Java对象转为一个Map,以及将map转为对应Java对象,代码如下:
public class BeanMapUtil {
private static ConcurrentHashMap<String, Map<String, String>> beanFieldCache = new ConcurrentHashMap<>();
public static Map<String, String> toMap(Object bean) {
if (bean == null) {
return null;
}
Map<String, String> map = Maps.newHashMap();
Map<String, String> beanFieldMap = getBeanFields(bean);
try {
for (Map.Entry<String, String> fieldEntry : beanFieldMap.entrySet()) {
String fieldName = fieldEntry.getKey();
String mapKey = fieldEntry.getValue();
map.put(mapKey, BeanUtils.getProperty(bean, fieldName));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return map;
}
public static void toBean(Map<String, String> map, Object result) {
if (map.isEmpty()) {
return;
}
try {
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
BeanUtils.setProperty(result, toBeanFieldName(key), value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private static Map<String, String> getBeanFields(Object bean) {
if (bean == null) {
return null;
}
if (beanFieldCache.contains(bean.getClass().getName())) {
return beanFieldCache.get(bean.getClass().getName());
}
Map<String, String> map = Maps.newHashMap();
for (Class clazz = bean.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
map.put(fieldName, toMapKey(fieldName));
}
}
beanFieldCache.put(bean.getClass().getName(), map);
return map;
}
private static String toMapKey(String fieldName) {
if (fieldName == null) {
return null;
}
StringBuilder underLineName = new StringBuilder();
for (int i = 0; i < fieldName.length(); i++) {
char c = fieldName.charAt(i);
if (Character.isUpperCase(c)) {
underLineName.append("_");
}
underLineName.append(upperCaseToLowerCase(c));
}
return underLineName.toString();
}
private static char upperCaseToLowerCase(char c) {
if (c >= 'A' && c <= 'Z') {
c += 32;
}
return c;
}
private static String toBeanFieldName(String key) {
if (key == null) {
return null;
}
StringBuilder camelFieldName = new StringBuilder();
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (c == '_' && i + 1 < key.length()) {
camelFieldName.append(lowerCaseToUpperCase(key.charAt(++i)));
} else {
camelFieldName.append(c);
}
}
return camelFieldName.toString();
}
private static char lowerCaseToUpperCase(char c) {
if (c >= 'a' && c <= 'z') {
c -= 32;
}
return c;
}
}
测试代码:
public class Parent {
private String parentField;
public String getParentField() {
return parentField;
}
public void setParentField(String parentField) {
this.parentField = parentField;
}
}
public class Child extends Parent {
private String childFieldA;
private String childFieldB;
private String childFieldC;
public String getChildFieldA() {
return childFieldA;
}
public void setChildFieldA(String childFieldA) {
this.childFieldA = childFieldA;
}
public String getChildFieldB() {
return childFieldB;
}
public void setChildFieldB(String childFieldB) {
this.childFieldB = childFieldB;
}
public String getChildFieldC() {
return childFieldC;
}
public void setChildFieldC(String childFieldC) {
this.childFieldC = childFieldC;
}
}
public class BeanMapTest {
public static void main(String[] args) {
Child child = new Child();
child.setParentField("p1");
child.setChildFieldA("c1");
child.setChildFieldB("c2");
child.setChildFieldC("c3");
Map<String, String> map = BeanMapUtil.toMap(child);
System.out.println(map);
Child result = new Child();
BeanMapUtil.toBean(map, result);
System.out.println(JSON.toJSONString(result));
}
}
运行结果:
{parent_field=p1, child_field_a=c1, child_field_c=c3, child_field_b=c2}
{"childFieldA":"c1","childFieldB":"c2","childFieldC":"c3","parentField":"p1"}