01.java中遍历实体类属性和类型
来源:互联网 发布:怎么改淘宝手机号 编辑:程序博客网 时间:2024/04/30 12:31
这个方法使用到了java.lang.reflect包的Field类,Method类。
此方法遍历实体类所有属性,对于常用的java包装类类型和java.util.Date,输出其属性值,如果有其它类型,则需自己扩展。
public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组 for(int j=0 ; j<field.length ; j++){ //遍历所有属性 String name = field[j].getName(); //获取属性的名字 System.out.println("attribute name:"+name); String type = field[j].getGenericType().toString(); //获取属性的类型 if(type.equals("class java.lang.String")){ //如果type是类类型,则前面包含"class ",后面跟类名 Method m = model.getClass().getMethod("get"+name); String value = (String) m.invoke(model); //调用getter方法获取属性值 if(value != null){ System.out.println("attribute value:"+value); } } if(type.equals("class java.lang.Integer")){ Method m = model.getClass().getMethod("get"+name); Integer value = (Integer) m.invoke(model); if(value != null){ System.out.println("attribute value:"+value); } } if(type.equals("class java.lang.Short")){ Method m = model.getClass().getMethod("get"+name); Short value = (Short) m.invoke(model); if(value != null){ System.out.println("attribute value:"+value); } } if(type.equals("class java.lang.Double")){ Method m = model.getClass().getMethod("get"+name); Double value = (Double) m.invoke(model); if(value != null){ System.out.println("attribute value:"+value); } } if(type.equals("class java.lang.Boolean")){ Method m = model.getClass().getMethod("get"+name); Boolean value = (Boolean) m.invoke(model); if(value != null){ System.out.println("attribute value:"+value); } } if(type.equals("class java.util.Date")){ Method m = model.getClass().getMethod("get"+name); Date value = (Date) m.invoke(model); if(value != null){ System.out.println("attribute value:"+value.toLocaleString()); } } } }
- 01.java中遍历实体类属性和类型
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型
- java中遍历实体类属性和类型
- 转载--java中遍历实体类属性和类型,属性值
- java中遍历实体类属性和类型,并赋值和获取值
- java中遍历实体类属性和类型、赋值和取值
- java日期以及使用Java反射机制遍历实体类的属性和类型
- java反射遍历实体类属性和类型,并赋值和获取值
- java反射遍历实体类属性和类型,并赋值和获取值
- 使用Java反射机制遍历实体类的属性和类型
- 使用java反射机制遍历实体类的属性和类型以及值
- 国内IC元器件市场集中度低 期待在线渠道解围
- iOS 静态度制作方法详细
- jquery弹出可拖动对话框+无刷新图片上传
- Linux 内存管理 -- 高端内存的映射方式
- 券商大幅下调诺基亚手机营收预期
- 01.java中遍历实体类属性和类型
- php中$_post()中的括号里填的是input的Name值,为什么不是Id值?
- 破产案或加速唯冠与苹果和解
- android上消息推送的实现
- 第05章 数组 04 练习2
- The Annotated Qt之QObject分析(1)
- JavaScript DOM高级程序设计笔记__(二)----第四章 响应用户操作和事件
- Httpclient默认连接数导致性能瓶颈问题
- mysql添加(删除)用户方法