oracle 查询转换位实体

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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值