bean
public class StudentBean {
private String name;
private int age;
private short status;
private long score;
private boolean sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public short getStatus() {
return status;
}
public void setStatus(short status) {
this.status = status;
}
public long getScore() {
return score;
}
public void setScore(long score) {
this.score = score;
}
public boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
return "StudentBean [name=" + name + ", age=" + age + ", status="
+ status + ", score=" + score + ", sex=" + sex + "]";
}
}
Util
public class Util {
public static Map<String, String> bean2map(Object javaBean) {
Map<String, String> result = new HashMap<String, String>();
Method[] methods = javaBean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("get")) {
String field = method.getName();
field = field.substring(field.indexOf("get") + 3);
field = field.toLowerCase().charAt(0) + field.substring(1); // 转换成小写
Object value = method.invoke(javaBean, (Object[]) null);
if (value != null) {
result.put(field, value.toString());
}
}
} catch (Exception e) {
}
}
return result;
}
public static void map2bean(Object javabean, Map<String, String> map) {
Method[] methods = javabean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("set")) {
String field = method.getName();
field = field.substring(field.indexOf("set") + 3);
String getterName="get"+field;
field = field.toLowerCase().charAt(0) + field.substring(1);
String value=map.get(field);
Method getter=javabean.getClass().getMethod(getterName);
String methodType=getter.getReturnType().getName();
Object[] cache = new Object[1];
if(methodType.equalsIgnoreCase("long")) {
cache[0] = new Long(value);
} else if(methodType.equalsIgnoreCase("int")||methodType.equalsIgnoreCase("integer")) {
cache[0] = new Integer(value);
} else if(methodType.equalsIgnoreCase("short")) {
cache[0] = new Short(value);
} else if(methodType.equalsIgnoreCase("float")) {
cache[0] = new Float(value);
} else if(methodType.equalsIgnoreCase("double")) {
cache[0] = new Double(value);
} else if(methodType.equalsIgnoreCase("boolean")) {
cache[0] = new Boolean(value);
} else if(methodType.equalsIgnoreCase("java.lang.String")) {
cache[0] = value;
} else if(methodType.equalsIgnoreCase("java.io.InputStream")) {
} else if(methodType.equalsIgnoreCase("char")) {
cache[0] = (Character.valueOf(value.charAt(0)));
}
method.invoke(javabean, cache);
}
} catch (Exception e) {
}
}
}
}
测试
public class Main {
public static void main(String[] args) {
StudentBean sBean=new StudentBean();
sBean.setName("小圆");
sBean.setAge(14);
sBean.setStatus((short) 1);
sBean.setScore(10050);
sBean.setSex(true);
Map<String,String> map=Util.bean2map(sBean);
System.out.println(map);
System.out.println("-----------------------------");
StudentBean s2=new StudentBean();
Util.map2bean(s2, map);
System.out.println(s2);
}
}