通過反射轉換java資料型別
阿新 • • 發佈:2018-12-02
最近專案中遇到通過類似hibernate又不是hibernate查詢出來的資料是一個List<Object[]>型別,不是自己想要的key,value型別,然後抽空寫了一個工具類,貌似這樣破壞了java的封裝性,標記一下,有不對的地方歡迎指出來
實體類:
public class User1 { private int id; private int age; private String name; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User1 [id=" + id + ", age=" + age + ", name=" + name + ", sex=" + sex + "]"; } }
工具類 SQLToEntityUtils
public class SQLToEntityUtils{ /** * @方法名稱: getListFromList * @功能描述: 從 List<Object[]> 轉換為 List<T> * @引數 :@param oldLst * @引數 :@param clazz * @引數 :@return * @返回型別:List<T> * @建立時間 :2017年5月8日上午11:55:14 */ public static <T> List<T> getListFromList(List<Object[]> oldLst,Class<T> clazz){ if (null == clazz || null == oldLst || oldLst.isEmpty() ) { return null; } List<T> newLst=new ArrayList<T>(); try { Field[] fields = clazz.getDeclaredFields(); if (null == fields) { return null; } for (int i=0 ; i<oldLst.size(); i++) { Object[] obj = oldLst.get(i); //實體類物件 T newObj = clazz.newInstance(); //遍歷賦值 for (int j = 0; j < obj.length; j++) { if (null !=obj[j]) { fields[j].setAccessible(true); fields[j].set(newObj, obj[j]); } } newLst.add(newObj); } return newLst; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } public static <T> T getObjectFromList(List<Object[]> oldLst,Class<T> clazz){ if (null == clazz || null == oldLst || oldLst.isEmpty() ) { return null; } //獲取到實體類物件 T newObj =null; try { newObj = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); if (null == fields) { return null; } Object[] obj = oldLst.get(0); //遍歷賦值 for (int j = 0; j < obj.length; j++) { if (null !=obj[j]) { fields[j].setAccessible(true); fields[j].set(newObj, obj[j]); } } return newObj; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { Object[] obj1=new Object[]{1,13,"zhangsan","男"}; Object[] obj2=new Object[]{1,13,"list","男"}; Object[] obj3=new Object[]{1,13,"王五","男"}; List<Object[]> list=new ArrayList<>(); list.add(obj1); list.add(obj2); list.add(obj3); // List<User1> userList = getListFromList(list, User1.class); User1 user2 = getObjectFromList(list, User1.class); System.out.println(user2.toString()); System.out.println(); // for (User1 user1 : userList) { // System.out.println(user1.toString()); // } // User1 user1 = getObjectFromList(list, User1.class); // Method[] methods = User1.class.getMethods(); // for (Method method : methods) { // System.out.println(method.getName()); // } Field[] fields = User1.class.getDeclaredFields(); // boolean flag=int instanceof(Integer); // Field.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { // System.out.println(fields[i].getName()); } } }