`
lan13217
  • 浏览: 495800 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

ResultToBean

 
阅读更多
package util;

import java.lang.reflect.Field;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.transform.ResultTransformer;

@SuppressWarnings("serial")
public class ResultToBean implements ResultTransformer {

    private Class entityClaz;

    public List transformList(List list) {
        return list;
    }

    /**
     * 日期处理
     */
    public Object transformTuple(Object[] values, String[] columns) {
        try {
            Object result = entityClaz.newInstance();
            for (int i = 0; i < columns.length; i++) {
                try {
                    String objectName = coloumnToObject(columns[i]);
                    Object value = values[i];
                    Field field;
                    //sql对象转换类添加支持设置继承的属性
					try {
						field = entityClaz.getDeclaredField(objectName);
					} catch (Exception e) {
						try {
							field = entityClaz.getSuperclass().getDeclaredField(objectName);
						}catch (Exception e1) {
							try {
								field = entityClaz.getSuperclass().getSuperclass().getDeclaredField(objectName);
							}catch (Exception e2) {
								field = entityClaz.getSuperclass().getSuperclass().getDeclaredField(objectName);
							}
						}
					}
                    field.setAccessible(true);
                    // 处理日期
                    if (field.getType() == java.util.Date.class
                            || field.getType() == java.sql.Timestamp.class
                            || field.getType() == java.sql.Date.class) {
                        
                        field.set(result, value);
                        continue;
                    }
                    // 其它类型处理
                    field.set(result, ReflectionUtils.convertValue(value, field
                            .getType()));

                } catch (Exception e) {
                    // fix
                	e.printStackTrace();
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final String coloumnToObject(String column) {
        String[] columnWords = column.split("_");
        String result = "";
        for (int i = 0; i < columnWords.length; i++) {
            if (i == 0) {
                result += StringUtils.lowerCase(columnWords[i]);
            } else {
                result += StringUtils.capitalize(StringUtils
                        .lowerCase(columnWords[i]));
            }
        }
        return result;
    }

    public static String toDBName(String objectName) {
        try {
            StringBuffer result = new StringBuffer();
            result.append(objectName.charAt(0));
            for (int i = 1; i < objectName.length(); i++) {
                char c = objectName.charAt(i);
                if (Character.isUpperCase(c)) {
                    result.append("_");
                }
                result.append(objectName.charAt(i));
            }
            return result.toString().toUpperCase();
        } catch (Exception e) {
            return objectName;
        }
    }

    public ResultToBean as(Class claz) {
        entityClaz = claz;
        return this;
    }

}



SQLQuery dataQuery = session.createSQLQuery(dataSql);
suitParames(queryObj, dataQuery);

dataQuery.setFirstResult(queryObj.getPageSize()*(queryObj.getPageIndex()));
dataQuery.setMaxResults(queryObj.getPageSize());
dataQuery.setResultTransformer(new ResultToBean().as(SfCard.class));
List<SfCard> resultList = (List<SfCard>) dataQuery.list();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics