通过反射转换java数据类型

来源:互联网 发布:手机足球软件 编辑:程序博客网 时间:2024/06/15 18:48

最近项目中遇到通过类似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;}@Overridepublic 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());}}}


2 0