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循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...
随机推荐
- 面向对象:MATLAB的自定义类 [MATLAB]
https://www.cnblogs.com/gentle-min-601/p/9785812.html 面向对象:MATLAB的自定义类 [MATLAB] 这几天刚刚开始学习MATLAB的面向 ...
- Lintcode: Knight Shortest Path
Given a knight in a chessboard (a binary matrix with 0 as empty and 1 as barrier) with a source posi ...
- nodejs 前端项目编译时内存溢出问题的原因及解决方案
现象描述 昨天用webpack打包Vue的项目时,node内存溢出而停止build项目,即是项目构建过程中频繁报内存溢出:FATAL ERROR: CALL_AND_RETRY_LAST Alloca ...
- vue中样式的典型操作(:class,:style)
<template> <div class="home-wrapper"> <div class="home-top">th ...
- Ubuntu14.04 安装 Sublime Text 3
Linux下安装,一种办法是从官网下载 tar.bz ,手动安装.另一种是使用apt-ge安装 这里介绍用 apt-get 自动安装方法: 1.添加sublime text 3的仓库: sudo ad ...
- netsh禁用启用本地连接
netsh interface set interface mi8 disablednetsh interface set interface mi8 enabled mi8是本地连接名称,需要管理员 ...
- 简单快速部署nexus3私服
本文适用范围:用户规模不大,不需要考虑maven仓库负载均衡的群体. 为何部署nexus3 之前由于懒某些原因,所有开发人员自己定义.m2的settings,大多使用ali提供的maven仓库,但是最 ...
- PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805318855278592 #include <iost ...
- zabbix3.4实现sendEmail邮件报警
一.安装软件 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 创建目录 mkdir /us ...
- 在linux和windows用c++编写c接口的动态库
linux 动态的头文件api.h #ifndef _API_H #define _API_H #ifdef DLL_IMPLEMENT #define DLL_EXPORT extern " ...