Java Bean
和Map
进行互转的方式有如下几种:
1.使用org.apache.commons.beanutils.BeanUtils
Bean转Map
PubOrgan organ = new PubOrgan();
organ.setOrganId("1");
organ.setOrganName("www.4spaces.org");
Map map = new BeanMap(organ);
Map转Bean
try {
PubOrgan organ = new PubOrgan();
Map<String,String> map = new HashMap();
map.put("organId", "1");
map.put("organName", "www.4spaces.org");
BeanUtils.populate(organ, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
2.使用com.alibaba.fastjson
参考: 使用fastjson实现Map和Java Bean互转;
3.使用jackson
参考: 将Java对象转换为Map对象;