package com.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
public class TestUtil {
public static Map<String, Object> PO2Map(Object o) throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
Field[] fields = null;
fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String proName = field.getName();
Object proValue = field.get(o);
map.put(proName, proValue);
}
return map;
}
public static Object map2PO(Map<String,Object> map,Object o) throws Exception{
if (!map.isEmpty()) {
for (String k : map.keySet()) {
Object v = "";
if (!k.isEmpty()) {
v = map.get(k);
}
Field[] fields = null;
fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
continue;
}
if (field.getName().equals(k)) {
field.setAccessible(true);
field.set(o, v);
}
}
}
}
return o;
}
public static void main(String[] args) throws Exception{
UserVo person=new UserVo();
person.setLoginName("cc");;
person.setEntId("3333");;
Map<String, Object> map=TestUtil.PO2Map(person);
System.out.println(map.get("loginName"));
UserVo u=new UserVo();
TestUtil.map2PO(map, u);
System.out.println(u.getEntId());
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
public class TestUtil {
public static Map<String, Object> PO2Map(Object o) throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
Field[] fields = null;
fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
String proName = field.getName();
Object proValue = field.get(o);
map.put(proName, proValue);
}
return map;
}
public static Object map2PO(Map<String,Object> map,Object o) throws Exception{
if (!map.isEmpty()) {
for (String k : map.keySet()) {
Object v = "";
if (!k.isEmpty()) {
v = map.get(k);
}
Field[] fields = null;
fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
continue;
}
if (field.getName().equals(k)) {
field.setAccessible(true);
field.set(o, v);
}
}
}
}
return o;
}
public static void main(String[] args) throws Exception{
UserVo person=new UserVo();
person.setLoginName("cc");;
person.setEntId("3333");;
Map<String, Object> map=TestUtil.PO2Map(person);
System.out.println(map.get("loginName"));
UserVo u=new UserVo();
TestUtil.map2PO(map, u);
System.out.println(u.getEntId());
}
}