首页 > 编程知识 正文

将对象转为map,把实体类转换为map

时间:2023-05-05 23:24:30 阅读:179627 作者:4607

//*

@ class name : queryvotobeanutil

@author:微微吟风

@description:TODO

@createDate: 2020/2/18

@version: 1.0

*/

公共类queryvotobeanutil {

//*

将JavaBean转换为地图

@param obj

@return

@throws SecurityException

@throws NoSuchMethodException

@ throwsinvocationtargetexception

@ throwsillegalargumentexception

@throws IllegalAccessException

*/

publicstaticmapbeantomap (objectobj ) throws NoSuchMethodException,SecurityException,IllegalAccessException,

IllegalArgumentException,InvocationTargetException {

创建//map集合

映射图=new hashmap (;

获取JavaBean的所有属性

Field[] fields=obj.getClass ().getDeclaredFields;

字段文件:字段(for ) {

//属性的第一个字符为大写string frist=fie.getname (.substring ),1 ).toUpperCase ); //要获取属性的类型Class? type=fie.getType (; //获取包属性字符串获取器=" "; if(boolean ).equals(type.getname ) ) { getter='is' frist fie.getName ) ).substring(1) 1; } else { getter=' get ' frist fie.getname ().substring(1) 1; 如何获取JavaBean method method=obj.getclass ().getmethod(getter,newclass ) {} ); 调用//方法,并返回返回值objectobjec=method.invoke(obj,new Object[] {} ); //判断返回值不是空if (objec!=null ) {map.put(fie.getname ),objec ); }else{map.put(fie.getname ),''); }

返回地图;

}

//*

将地图转换为JavaBean

@param map

@param obj

@return

@throws SecurityException

@throws NoSuchMethodException

@ throwsinvocationtargetexception

@ throwsillegalargumentexception

@throws IllegalAccessException

*/

公共静态对象映射(mapstring,objectmap,Object obj ) throws NoSuchMethodException,SecurityException,

IllegalAccessException,IllegalArgumentException,InvocationTargetException {

获取JavaBean的所有属性

Field[] field=obj.getClass ().getDeclaredFields;

for(fieldfi:field ) {

判断//key值是否存在

string fildname=addstringxhx (fi.getname ) );

映射内容密钥(fildname ) )。

获取//key的值

stringvalue=map.get(fildname ).toString ); //属性的第一个字符为大写string frist=fi.getname (.substring ),1 ).toUpperCase ); //属性封装set方法string setter=' set ' frist fi.getname ().substring(1) ) 1; //获取当前属性类型Class吗? type=fi.getType (; 获取JavaBean的方法,并使用类型Method method=obj.getClass ().getmethod(setter,type ); //判定属性是double类型if (Java.lang.string.equals ) type.getname ) ) /调用当前Javabean的set方法,并调用指定的类型参数method.invon } else if (' double '.equals (type.getname ) ) (method.invoke ) obj,double.valueof ) (value ); } else if (' char '.equals (type.getname ) ) (method.invoke ) obj,value ); } }

返回对象;

}

privatestaticstringaddstringxhx {

stringbuilder stringbuilder=new stringbuilder (name.length ) ) 5; int len=name.length (; for(intI=0; ilen; I ) charc=name.charat(I; if(character.isuppercase(c ) ) stringbuilder.append (' _ ' character.to lower case (c ) ); }else{stringbuilder.append(c; } } return stringBuilder.toString (; }

//*

将列表映射,对象转换为列表

@param listm

@param obj

@return

@ throwsinvocationtargetexception

@ throwsillegalargumentexception

@throws IllegalAccessException

@throws SecurityException

@throws NoSuchMethodException

*/

publicstaticlistlistmaptolistbean (listmaps tring,Object listm,Object obj ) )。

throws NoSuchMethodException、SecurityException、IllegalAccessException、IllegalArgumentException、

InvocationTargetException {

List list=new ArrayList (;

//循环遍历贴图对象

for (映射,Object m : listm )。

调用用于将//map转换为JavaBean的方法

objectobjs=maptobean(m,obj );

添加到list集合

list.add(objs;

() ) ) ) )。

返回列表;

() ) ) ) )。

//*

将列表转换为列表

@param list

@return

@throws NoSuchMethodException

@throws SecurityException

@throws IllegalAccessException

@ throwsillegalargumentexception

@ throwsinvocationtargetexception

*/

公共静态列表映射,objectlistbeantolistmap (list list ) throws NoSuchMethodException,

SecurityException、IllegalAccessException、IllegalArgumentException、InvocationTargetException {

ListMapString,objectlistmap=newarraylistmapstring,object (;

for (对象:列表) {

listmap.add(beantomap(ob ) ); () ) ) ) )。

return listmap;

() ) ) ) )。

() ) ) ) )。

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