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循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...
随机推荐
- Python基础之数组和向量化计算总结
一.多维数组 1.生成ndarray (array函数) .np.array()生成多维数组 例如:import numpy as npdata1=[6,7.5,8,0,1] #创建简 ...
- Python科学计算学习之高级数组(二)
代码性能和向量化 背景:Python是一种解释型的编程语言,基本的python代码不需要任何中间编译过程来得到机器代码,而是直接执行.而对于C.C++等编译性语言就需要在执行代码前将其编译为机器指令. ...
- 爬虫---爬虫er与反爬虫er之间的斗争 转发
转自:昵称:python修行路 https://www.cnblogs.com/zhaof/p/7326260.html
- docker容器的实践——综合项目一
Docker 综合实验 实验拓扑: [调度器] Keepalived + nginx 一.Keepalived服务的安装配置: 关闭LVS服务器的ipv4代理和 ...
- C# 合并只要有交集的所有集合
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Respone弹窗
Response.Write("<script>window.open('default.aspx?iID=" + GridView1.DataKeys[GridVie ...
- c++ 单元测试框架 gmock 深度剖析
c++ 单元测试框架 gmock 深度剖析 随着微服务和CI的流行,在目前的软件工程领域中单元测试可以说是必不可少的一个环节,在TDD中,单元测试更是被提高到了一个新的高度.但是很多公司由于很多不同的 ...
- Web前端培训教程:CSS3动画怎么实现的
动画 CSS3属性中有关于制作动画的三个属性: transform,transition,animation keyframes @keyframes mymove{ from{初始状态属性} to{ ...
- Windows7 安装TensorFlow,python3.6
TensorFlow 1.2.0新版本完美支持Python3.6,windows在cmd中输入pip install tensorflow就能下载应用最新tensorflow 只需在cmd中输入pip ...
- 3.JAVA基础复习——JAVA中的类与对象
什么是对象: 就是现实中真实的实体,对象与实体是一一对应的,现实中每一个实体都是一个对象在. JAVA中的对象: Java中通过new关键字来创建对象. 类: 用JAVA语言对现实生活中的事物进行描述 ...