java中循环遍历实体类的属性和数据类型以及属性值
package com.walkerjava.test; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date; /***
* 遍历实体类的属性和数据类型以及属性值
*
* @author LiBaozhen
* @date 2013-1-4 上午10:25:02
* @company
* @version v1.3
* @see 相关类
* @since 相关/版本
*/
public class ReflectTest {
public static void reflectTest(Object model) throws NoSuchMethodException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
// 获取实体类的所有属性,返回Field数组
Field[] field = model.getClass().getDeclaredFields();
// 遍历所有属性
for (int j = 0; j < field.length; j++) {
// 获取属性的名字
String name = field[j].getName();
// 将属性的首字符大写,方便构造get,set方法
name = name.substring(0, 1).toUpperCase() + name.substring(1);
// 获取属性的类型
String type = field[j].getGenericType().toString();
// 如果type是类类型,则前面包含"class ",后面跟类名
System.out.println("属性为:" + name);
if (type.equals("class java.lang.String")) {
Method m = model.getClass().getMethod("get" + name);
// 调用getter方法获取属性值
String value = (String) m.invoke(model);
System.out.println("数据类型为:String");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
if (type.equals("class java.lang.Integer")) {
Method m = model.getClass().getMethod("get" + name);
Integer value = (Integer) m.invoke(model);
System.out.println("数据类型为:Integer");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
if (type.equals("class java.lang.Short")) {
Method m = model.getClass().getMethod("get" + name);
Short value = (Short) m.invoke(model);
System.out.println("数据类型为:Short");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
if (type.equals("class java.lang.Double")) {
Method m = model.getClass().getMethod("get" + name);
Double value = (Double) m.invoke(model);
System.out.println("数据类型为:Double");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
if (type.equals("class java.lang.Boolean")) {
Method m = model.getClass().getMethod("get" + name);
Boolean value = (Boolean) m.invoke(model);
System.out.println("数据类型为:Boolean");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
if (type.equals("class java.util.Date")) {
Method m = model.getClass().getMethod("get" + name);
Date value = (Date) m.invoke(model);
System.out.println("数据类型为:Date");
if (value != null) {
System.out.println("属性值为:" + value);
} else {
System.out.println("属性值为:空");
}
}
}
}
}
http://blog.csdn.net/dongzhouzhou/article/details/8659836
- package com.walkerjava.test;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Date;
- /***
- * 遍历实体类的属性和数据类型以及属性值
- *
- * @author LiBaozhen
- * @date 2013-1-4 上午10:25:02
- * @company
- * @version v1.3
- * @see 相关类
- * @since 相关/版本
- */
- public class ReflectTest {
- public static void reflectTest(Object model) throws NoSuchMethodException,
- IllegalAccessException, IllegalArgumentException,
- InvocationTargetException {
- // 获取实体类的所有属性,返回Field数组
- Field[] field = model.getClass().getDeclaredFields();
- // 遍历所有属性
- for (int j = 0; j < field.length; j++) {
- // 获取属性的名字
- String name = field[j].getName();
- // 将属性的首字符大写,方便构造get,set方法
- name = name.substring(0, 1).toUpperCase() + name.substring(1);
- // 获取属性的类型
- String type = field[j].getGenericType().toString();
- // 如果type是类类型,则前面包含"class ",后面跟类名
- System.out.println("属性为:" + name);
- if (type.equals("class java.lang.String")) {
- Method m = model.getClass().getMethod("get" + name);
- // 调用getter方法获取属性值
- String value = (String) m.invoke(model);
- System.out.println("数据类型为:String");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- if (type.equals("class java.lang.Integer")) {
- Method m = model.getClass().getMethod("get" + name);
- Integer value = (Integer) m.invoke(model);
- System.out.println("数据类型为:Integer");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- if (type.equals("class java.lang.Short")) {
- Method m = model.getClass().getMethod("get" + name);
- Short value = (Short) m.invoke(model);
- System.out.println("数据类型为:Short");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- if (type.equals("class java.lang.Double")) {
- Method m = model.getClass().getMethod("get" + name);
- Double value = (Double) m.invoke(model);
- System.out.println("数据类型为:Double");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- if (type.equals("class java.lang.Boolean")) {
- Method m = model.getClass().getMethod("get" + name);
- Boolean value = (Boolean) m.invoke(model);
- System.out.println("数据类型为:Boolean");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- if (type.equals("class java.util.Date")) {
- Method m = model.getClass().getMethod("get" + name);
- Date value = (Date) m.invoke(model);
- System.out.println("数据类型为:Date");
- if (value != null) {
- System.out.println("属性值为:" + value);
- } else {
- System.out.println("属性值为:空");
- }
- }
- }
- }
- }
java中循环遍历实体类的属性和数据类型以及属性值的更多相关文章
- java中如何遍历实体类的属性和数据类型以及属性值
package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...
- JAVA中循环遍历list有三种方式
转自:https://blog.csdn.net/changjizhi1212/article/details/81036509JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常 ...
- Java中entity(实体类)的写法规范
在日常的Java项目开发中,entity(实体类)是必不可少的,它们一般都有很多的属性,并有相应的setter和getter方法.entity(实体类)的作用一般是和数据表做映射.所以快速写出规范的e ...
- JAVA 中 Map 与实体类相互转换的简单方法
1. 在 pom.xml 中引入依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId> ...
- java遍历实体类的属性和数据类型以及属性值
遍历实体类的树形和数据类型一级属性值 /** * 遍历实体类的属性和数据类型以及属性值 * @param model * @throws NoSuchMethodException * @throws ...
- Java中循环删除list中元素的方法总结
印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后在今天使用的时候发现报错了,然后去科普了一下,发现这是一个误区.下面我们来一起看一下. Java中循环遍 ...
- JAVA中循环删除list中元素的方法总结【转】
印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...
- JAVA中循环删除list中元素
文章来源: https://www.cnblogs.com/pcheng/p/5336903.html JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环) ...
- JAVA中循环删除list中元素的方法总结(同上篇)
印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...
随机推荐
- cocos creator 重写源码按钮Button点击音频封装
(function(){ var Super = function(){}; Super.prototype = cc.Button.prototype; //实例化原型 Super.prototyp ...
- 一张图解释IaaS,PaaS,SaaS
图片来源于MVA教程:快速入门——面向IT专业人员的Windows Azure IaaS
- mat-form-field must contain a MatFormFieldControl错误的解决方法
下面的代码竟然出错了: <mat-form-field> <input matInput placeholder="输入名称"> </mat-form ...
- U-net网络实现医学图像分割以及遥感图像分割源代码
U-net网络主要思路是源于FCN,采用全卷积网络,对图像进行逐像素分类,能在图像分割领域达到不错的效果. 因其网络结构类似于U型,所以以此命名,可以由其架构清晰的看出,其构成是由左端的卷积压缩层,以 ...
- HTML页面空格记录     (小计)
半角的不断行的空白格(推荐使用) 也就是咱们经常在英文状态下面使用的空格按键 半角的空格 他的宽度为中文字符的一半长度 全角的空格他的宽度为中文字符的长度
- [小程序] 微信小程序 picker 中range-key中必须带单引号
原文地址:http://blog.csdn.net/u012329294/article/details/74906504 <view class="section"> ...
- C# 同步更新网盘和本地的文件夹及文件
该程序是可以更新本地文件或更新网盘文件或者网盘和本地同步更新 下载地址:https://files.cnblogs.com/files/Wonderful-Life/UpdateFilesSync.r ...
- linux创建桌面快捷方式
这里拿postman举例,其他的程序类似 在/usr/sharp/applications新建postman.desktop文件(终端下输入vim /usr/sharm/applications/po ...
- python 科学计算及数据可视化
第一步:利用python,画散点图. 第二步:需要用到的库有numpy,matplotlib的子库matplotlib.pyplot numpy(Numerical Python extensions ...
- winform Combobox出现System.Data.DataRowView的解决的方法
个人总结: 1.触发了SelectedIndexChanged事件时:comboBox1.DataSource = dt;要放在comboBox1.SelectedIndex = 0;的上面 comb ...