hibernate中把查询出来的集合封装为对象集合
2017-09-01 11:00
435 查看
public static List objectToList(List obj, Class clazz) throws Exception { List list = new ArrayList(); for (Object result : obj) { int i = 0; // 获取结果集中每条记录的数据 Object[] values = (Object[]) result; // 通过对象属性进行数据注入 Field[] f = clazz.getDeclaredFields(); Object o = clazz.newInstance(); for (Field field : f) { char c = field.getName().charAt(0); c -= 32; String mname = c + field.getName().substring(1); mname = "set" + mname; Class<?> type = field.getType(); Method md = clazz.getMethod(mname, type); if (i < values.length) { String value = values[i].toString().trim(); if (type == String.class) { md.invoke(o, values[i] != null ? value : null); } else{ if (type == Integer.class) { md.invoke(o,Integer.valueOf(value)); } else if (type == Date.class) { DateTimeFormat format = (field .getAnnotation(DateTimeFormat.class)); SimpleDateFormat sdf = new SimpleDateFormat( format.pattern()); md.invoke(o,sdf.parse(value) ); } else if (type == BigDecimal.class) { md.invoke(o, new BigDecimal(value)); } else if (type == Long.class) { md.invoke(o,Long.parseLong(value)); } else if (type == Short.class) { md.invoke(o,Short.parseShort(value)); } } } i++; } list.add(o); } return list; }
相关文章推荐
- Hibernate 将表连接查询和根据条件查询出来的对象封装成另一个对象。
- hibernate--原生SQL查询无法返回对象集合
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- hibernate将本地SQL查询结果封装成对象
- hibernate的sql查询出来封装实体的两种方式
- hibernate使用list映射,查询出来的list集合包含多个null记录
- MongoDB学习笔记~自己封装的Curd操作(查询集合对象属性,更新集合对象)
- hibernate将本地SQL查询结果封装成对象
- Hibernate查询条件封装对象Expression介绍
- 在Eclipse中测试MySQL-JDBC(5)查询1-4之间的所有员工,并且封装为一个个的employee对象,并且存储到一个集合中
- hibernate Query对象查询集合
- (转)Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- 从Object对象中封装了其它对象,怎样取出呢?(Hibernate查询结果处理)
- hibernate 使用sql 查询利用setResultTransformer查询出封装好的VO对象
- hibernate中查询出来的字段并不能自动转换为bean对象的解决方法
- 将具有关联关系的两个表从hibernate查询出来转成json对象时报错
- 解决Hibernate原生SQL映射问题 - SQL查询出来的结果映射为值对象
- Hibernate中关于多表连接查询hql 和 sql 返回值集合中对象问题
- Hibernate根据查询内容封装成对象(DTO)
- Hibernate 通过批量ID进行查询所对应的对象集合