java遍歷實體類的屬性和資料型別以及屬性值
阿新 • • 發佈:2019-02-12
和同學接了個外包的活,由於專案中很多地方要用到poi匯出excel,而每次匯出都要寫很多相同的程式碼,因為poi的cell.setCellValue();每次設定的都是不同實體bean的屬性值,導致程式碼裡很多重複的值,我在想有沒有可以自動裝載bean的屬性及屬性值的方法。首先想到的肯定是反射,但是自己寫了一下沒寫出來,so上網查了一下,發現了這個方法,感覺不錯,就記錄下來了。原文連結http://blog.csdn.net/dongzhouzhou/article/details/8446782
原文的程式碼如下
/** * 遍歷實體類的屬性和資料型別以及屬性值 * @param model * @throws NoSuchMethodException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ 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("屬性值為:空"); } } } }
由於我的實體bean裡有double型別,我又不想用其封裝類的方法獲取值,於是我在他的基礎上又加入了一段程式碼,
if (type.equals("double")) { Method m = model.getClass().getMethod("get" + name); double value = (double) m.invoke(model); System.out.println("資料型別為:double"); if (value >0) { System.out.println("屬性值為:" + value); } else { System.out.println("屬性值為:空"); } } System.out.println("屬性型別為:"+type);
這樣一來,就可以知道屬性型別是什麼,可以很好的加判斷語句了。當然原文程式碼還有很多值得優化的地方,我這裡就沒有進行優化,準備等我將它與poi匯出整合之後再來優化。