package com.experian.app.common.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.experian.activity.util.BeanUtil;
public class JavaBeanConvert {
/**
* Oracle 字段转化为对象
* @param clazz
* @param maps
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List convert(Class clazz ,List<Map<String,Object>> maps) throws Exception{
List list = new ArrayList();
Field[] fds = clazz.getDeclaredFields();
Field[] superfds ={};
if(clazz.getSuperclass()!=null){
superfds = clazz.getSuperclass().getDeclaredFields();
}
for(int i=0;i<maps.size();i++){
Object dto =clazz.newInstance();
Map<String,Object> map = maps.get(i);
for(int k=0;k<fds.length;k++){
Field fd = fds[k];
String fieldName =fd.getName();
Object fieldValue = map.get(fieldName.toUpperCase());
if(fd.getType().equals(BigDecimal.class) && null == fieldValue){
fieldValue = BigDecimal.ZERO;
}
if(!(fd.getType().equals(Date.class) && null == fieldValue)
&& !(fd.getType().equals(Timestamp.class) && null == fieldValue)){
org.apache.commons.beanutils.BeanUtils.setProperty(dto, fieldName, fieldValue);
}
}
if(superfds.length>0){
for(int k=0;k<superfds.length;k++){
Field fd = superfds[k];
String fieldName =fd.getName();
Object fieldValue = map.get(fieldName.toUpperCase());
if(fd.getType().equals(BigDecimal.class) && null == fieldValue){
fieldValue = BigDecimal.ZERO;
}
if(!(fd.getType().equals(Date.class) && null == fieldValue)
&& !(fd.getType().equals(Timestamp.class) && null == fieldValue)){
org.apache.commons.beanutils.BeanUtils.setProperty(dto, fieldName, fieldValue);
}
}
}
list.add(dto);
}
return list;
}
/**
* 构造hibernate参数
* @param obj
* @return
*/
@SuppressWarnings("unchecked")
public static DetachedCriteria getDetachedCriteria(Object obj){
DetachedCriteria dc = DetachedCriteria.forClass(obj.getClass());
Map<String, Object> map=BeanUtil.getFiledsInfo(obj);
for (Map.Entry<String, Object> entry : map.entrySet()) {
dc.add(Restrictions.like(entry.getKey(), entry.getValue()));
}
dc.addOrder(Order.desc("createTime"));
return dc;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static DetachedCriteria getDetachedCriteria(Class clazz,Object obj){
DetachedCriteria dc = DetachedCriteria.forClass(clazz);
Map<String, Object> map=BeanUtil.getFiledsInfo(obj);
for (Map.Entry<String, Object> entry : map.entrySet()) {
dc.add(Restrictions.like(entry.getKey(), entry.getValue()));
}
dc.addOrder(Order.desc("createTime"));
return dc;
}
}
SQLQuery query = withholdDetailDao.getCurrSession().createSQLQuery(sql.toString());
List<Map<String,Object>> maps = query.list();List<WithholdContractVO> list =JavaBeanConvert.convert(WithholdContractVO.class, maps);