首页 > 编程知识 正文

map转object对象,java实体类转成一个map

时间:2023-05-04 02:40:45 阅读:17257 作者:2462

译文: 3358 www.open-open.com/code/view/1423280939826

/* *使用org.Apache.com mons.beanutils获取SSD yz/class a {公共静态对象映射,类? beanclass(throwsexception(if ) map==null )返回空值; objectobj=bean class.new instance (; org.Apache.com mons.beanutils.beanutils.populate (obj,map ); 返回对象; }公共静态地图? objecttomap(objectobj ) if ) obj==null )返回空值; 使用return new org.Apache.com mons.beanutils.bean map (obj ) }/***introspector执行SSD yz/classb {公共静态对象映射beanclass(throwsexception(if ) map==null )返回空值; objectobj=bean class.new instance (; beaninfobeaninfo=introspect or.getbeaninfo (obj.getclass ) ); property descriptor [ ] property descriptors=bean info.getproperty descriptors (; 属性描述符描述符(for ) ({ method setter=property.getwritemethod ); if(Setter!=null(setter.invoke(obj,map.get ) property.getname ) ); } }返回对象; }公共静态映射,对象对象对象时间图(对象) throwsexception(if ) obj==null )返回空值; 映射,Object map=new HashMapString,Object (; beaninfobeaninfo=introspect or.getbeaninfo (obj.getclass ) ); property descriptor [ ] property descriptors=bean info.getproperty descriptors (; 属性描述符propertydescriptorproperty 3360属性描述符({ string key=property.getname ); if (key.compare to ignore case (' class ' )==0) { continue; } method getter=property.getreadmethod (; Object value=getter!=null? Getter.invoke(obj ) : null; map.put(key,value ); }返回图; 使用}//***reflect获取SSD yz/class c {公共静态对象映射(映射,对象映射,类? beanclass(throwsexception(if ) map==null )返回空值; objectobj=bean class.new instance (; Field[] fields=obj.getClass ().getDeclaredFields ); for (field field : fields (intmod=field.get modifiers ); if(modifier.isstatic(mod ) ) { continue; }field.setaccessible(true; field.set(obj,map.get ) field.getname ) ); }返回对象; }公共静态映射,对象对象对象时间图(对象) throwsexception(if ) obj==null ) {返回空值; }映射,Object map=new HashMapString,Object (; field [ ] declared fields=obj.getclass ().getDeclaredFields ); 现场字段: declared fields (for ) field.setaccessible(true ); map.put(field.getname )、field.get )和obj ); }返回图; }能干的蜗牛p能干的蜗牛/p

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。