JAVAMap转换为Bean或VO.docx_第1页
JAVAMap转换为Bean或VO.docx_第2页
JAVAMap转换为Bean或VO.docx_第3页
JAVAMap转换为Bean或VO.docx_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JAVA Map转换为Bean或VOJava.util 中的集合类包含 Java 中某些最常用的类。Map 提供了一个更通用的元素存储方法。Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值,在java编程中使用的相当之多。但是当我们进行业务逻辑或数据库处理时,往往应用的是自己框架独有的Bean或VO来存储数据,这就需要我们应用一个公共方法来将map中存储的数据转换为相对应的Bean或VO,主要用到技术就是java的反射机制。具体代码如下:/该方法主要传入的参数有两个,第一个是Map接口,第二个就是要绑定的VO。public static void mapBind(Map map,PmsBaseVO pmsVo) throws Exception/获得传入vo的Class方法Class newClass = pmsVo.getClass(); /得到vo中所有的成员变量Field fs = newClass.getDeclaredFields();/方法变量 String methodName = null; /map的value值 Object mapValue = null; /参数类型 String parameterType = null; /查找方法时需要传入的参数 Class parameterTypes = new Class1; /执行invoke方法时需要传入的参数 Object args = new Object1; /取得Map的迭代器 Iterator it = map.keySet().iterator(); while (it.hasNext() /取出map的key值 String key = (String)it.next(); if(key != null) for(int i=0;ifs.length;i+) if(key.equals(fsi.getName() /拼set方法名 methodName = set + key.replaceFirst(key.substring(0, 1),key.substring(0, 1).toUpperCase(); try /得到vo中成员变量的类型 parameterTypes0 = fsi.getType(); parameterType = parameterTypes0.toString(); /找到vo中的方法 Method method = newClass.getDeclaredMethod(methodName,parameterTypes); mapValue = map.get(key);/下面代码都是参数类型是什么,如果有需求可以自行增加 /当set方法中的参数为int或者Integer if(parameterTypes0 = Integer.class | parameterTypes0 = int.class) if(mapValue instanceof Integer) args0 = mapValue; else args0 = Integer.parseInt(String)mapValue); /当set方法中的参数为Date else if(parameterTypes0 = Date.class) if(mapValue instanceof Date) args0 = mapValue; else SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd);args0 = sdf.parse(String)mapValue); /当set方法中的参数为Float else if(parameterTypes0 = double.class | parameterTypes0 = Double.class) if(mapValue instanceof Double) args0 = mapValue; elseargs0 = Double.parseDouble(String)mapValue); /当set方法中的参数为其他 else if( parameterTypes0 = String.class) if(mapValue instanceof String) String tempArray = (String)mapValue; String result = ; for(int m=0;mtempArray.length;m+) result = result + tempArraym + ,; result = result.substring(0, result.length()-1); args0 = result; elseargs0 = (String)mapValue; else args0 = mapValue; /执行set方法存储数据method.invoke(pmsVo, args); catch (SecurityException e) throw new SecurityException(mapBind安全异常:+ e); catch (NoSuchMethodException e) throw new NoSuchMethodException(mapBindVo中无此方法异常 + e); catch (IllegalArgumentException e) throw new Exception(VO中+key+属性类型+parameterType+与Map中值为+mapValue+的类型不匹配); catch (IllegalAccessException e) throw new IllegalAc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论