zoukankan      html  css  js  c++  java
  • 【转】java遍历实体类的属性和数据类型以及属性值

    和同学接了个外包的活,由于项目中很多地方要用到poi导出excel,而每次导出都要写很多相同的代码,因为poi的cell.setCellValue();每次设置的都是不同实体bean的属性值,导致代码里很多重复的值,我在想有没有可以自动装载bean的属性及属性值的方法。首先想到的肯定是反射,但是自己写了一下没写出来,so上网查了一下,发现了这个方法,感觉不错,就记录下来了。原文链接http://blog.csdn.net/dongzhouzhou/article/details/8446782

    原文的代码如下

     
    1. /** 
    2.  * 遍历实体类的属性和数据类型以及属性值 
    3.  * @param model 
    4.  * @throws NoSuchMethodException 
    5.  * @throws IllegalAccessException 
    6.  * @throws IllegalArgumentException 
    7.  * @throws InvocationTargetException 
    8.  */  
    9. public static void reflectTest(Object model) throws NoSuchMethodException,  
    10.                 IllegalAccessException, IllegalArgumentException,  
    11.                 InvocationTargetException {  
    12.     // 获取实体类的所有属性,返回Field数组  
    13.     Field[] field = model.getClass().getDeclaredFields();  
    14.     // 遍历所有属性  
    15.     for (int j = 0; j < field.length; j++) {  
    16.             // 获取属性的名字  
    17.             String name = field[j].getName();  
    18.             // 将属性的首字符大写,方便构造get,set方法  
    19.             name = name.substring(0, 1).toUpperCase() + name.substring(1);  
    20.             // 获取属性的类型  
    21.             String type = field[j].getGenericType().toString();  
    22.             // 如果type是类类型,则前面包含"class ",后面跟类名  
    23.             System.out.println("属性为:" + name);  
    24.             if (type.equals("class java.lang.String")) {  
    25.                     Method m = model.getClass().getMethod("get" + name);  
    26.                     // 调用getter方法获取属性值  
    27.                     String value = (String) m.invoke(model);  
    28.                     System.out.println("数据类型为:String");  
    29.                     if (value != null) {  
    30.                             System.out.println("属性值为:" + value);  
    31.                     } else {  
    32.                             System.out.println("属性值为:空");  
    33.                     }  
    34.             }  
    35.             if (type.equals("class java.lang.Integer")) {  
    36.                     Method m = model.getClass().getMethod("get" + name);  
    37.                     Integer value = (Integer) m.invoke(model);  
    38.                     System.out.println("数据类型为:Integer");  
    39.                     if (value != null) {  
    40.                             System.out.println("属性值为:" + value);  
    41.                     } else {  
    42.                             System.out.println("属性值为:空");  
    43.                     }  
    44.             }  
    45.             if (type.equals("class java.lang.Short")) {  
    46.                     Method m = model.getClass().getMethod("get" + name);  
    47.                     Short value = (Short) m.invoke(model);  
    48.                     System.out.println("数据类型为:Short");  
    49.                     if (value != null) {  
    50.                             System.out.println("属性值为:" + value);  
    51.                     } else {  
    52.                             System.out.println("属性值为:空");  
    53.                     }  
    54.             }  
    55.             if (type.equals("class java.lang.Double")) {  
    56.                     Method m = model.getClass().getMethod("get" + name);  
    57.                     Double value = (Double) m.invoke(model);  
    58.                     System.out.println("数据类型为:Double");  
    59.                     if (value != null) {  
    60.                             System.out.println("属性值为:" + value);  
    61.                     } else {  
    62.                             System.out.println("属性值为:空");  
    63.                     }  
    64.             }  
    65.             if (type.equals("class java.lang.Boolean")) {  
    66.                     Method m = model.getClass().getMethod("get" + name);  
    67.                     Boolean value = (Boolean) m.invoke(model);  
    68.                     System.out.println("数据类型为:Boolean");  
    69.                     if (value != null) {  
    70.                             System.out.println("属性值为:" + value);  
    71.                     } else {  
    72.                             System.out.println("属性值为:空");  
    73.                     }  
    74.             }  
    75.             if (type.equals("class java.util.Date")) {  
    76.                     Method m = model.getClass().getMethod("get" + name);  
    77.                     Date value = (Date) m.invoke(model);  
    78.                     System.out.println("数据类型为:Date");  
    79.                     if (value != null) {  
    80.                             System.out.println("属性值为:" + value);  
    81.                     } else {  
    82.                             System.out.println("属性值为:空");  
    83.                     }  
    84.             }  
    85.     }  
    86. }  

    由于我的实体bean里有double类型,我又不想用其封装类的方法获取值,于是我在他的基础上又加入了一段代码,

     
    1. if (type.equals("double")) {  
    2.     Method m = model.getClass().getMethod("get" + name);  
    3.     double value = (double) m.invoke(model);  
    4.     System.out.println("数据类型为:double");  
    5.     if (value >0) {  
    6.             System.out.println("属性值为:" + value);  
    7.     } else {  
    8.             System.out.println("属性值为:空");  
    9.     }  
    10. }  
    11. System.out.println("属性类型为:"+type);  


    这样一来,就可以知道属性类型是什么,可以很好的加判断语句了。当然原文代码还有很多值得优化的地方,我这里就没有进行优化,准备等我将它与poi导出整合之后再来优化。

  • 相关阅读:
    2.1 String的特性
    sql语句对列进行操作
    java 集合排序
    C#抽象类及其方法的学习
    haproxy+keepalived实现高可用负载均衡
    RPC框架简易实现
    WCF中的ServiceHost初始化两种方式
    C# Stopwatch与TimeSpan详解
    HBase入门篇
    nginx+tomcat+redis完成session共享
  • 原文地址:https://www.cnblogs.com/xujishou/p/7088232.html
Copyright © 2011-2022 走看看