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循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...
随机推荐
- .NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理?(转)
.NET[C#]中NullReferenceException(未将对象引用到实例)是什么问题?如何修复处理? 后端开发 作者: Rector 1973 阅读 0 评论 0 收藏 收藏本文 ...
- MongoDB基本信息
一.MongoDB简介 来源:在2007年,由纽约一个叫10gen的创业团队开发,公司现在叫做MongoDB Inc,最初被开发为PAAS(平台即服务). 数据库类型:基于分布式文件存储的数据库.由C ...
- Sublime text 替换成对标签 首尾匹配的HTML 标签
按Cmd-Shift-K (Win: Ctrl-Shift-') 就能选中这组标签
- CentOS 7 Nginx1.12.2平滑升级到新版本nginx-1.13.3
查看当前Nginx版本信息 [root@web ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/ built by gcc (Red H ...
- 极致21点开发DAY3
今天完成的主要任务是活动窗口的显示与关闭,以及领取金币的逻辑.用到了数据持久化技术.我想记录的主要是领取金币的逻辑. 领取金币算法:如果今天没有领取金币,即可领取,否则什么都不做. 一句话描述足矣,但 ...
- 04-python3.5-模拟三级菜单-省-县-区域--01
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:XZ data = { '北京':{ "昌平":{ "沙河&qu ...
- Winform 图片预览列表+分页显示
针对图片列表展示信息,一开始没有做过相关类似的功能,大多都是以表格行显示为主,所以刚开始实现这个功能的时候是懵逼的.无从下口.在网上搜索一时半会也没找到合适的解决方案.大致就是类似于下图这样,每条数据 ...
- mysql的group_concat列转行函数
SELECT auditor,sum(count) total, GROUP_CONCAT(type,'=', count) AS type_count FROM auditor_dm_ol GROU ...
- coredump文件抓取设置
ulimit -c unlimitedecho 1 > /proc/sys/kernel/core_uses_pidecho "/tmp/core-%e-%s-%u-%g-%p-%t& ...
- Win7 指定以某个用户运行某个程式
登陆的是用户A,想要以用户B执行某个程式,可以在cmd命令符下执行以下语句 runas /user:Domain\UserB /savecred notepad.exe 说明:/user:的后面即为 ...