import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import xml.dao.admin.AdminArea;
public class NativeSQLQuery extends HibernateDaoSupport {
/**
* 将数据库查询出的数据转化为AdminArea
* @param queryString 查询admin_area表数据的sql语句
* @param pojoClass
* @param jdbcTemplate
* @return
*/
public List<? extends AdminArea> findObjectBySql(String queryString,
Class<? extends AdminArea> pojoClass, JdbcTemplate jdbcTemplate) {
Session session = this.getSession();
/**
* 使用SQL构造查询对象,此SQL是可以被JDBC接受的SQL,如SELECT * FROM XXX_TABLE
*/
Query query = session.createSQLQuery(queryString);
/**
* 设置结果集转换器
*/
query.setResultTransformer(new SqlColumnToBean(pojoClass));
List list = query.list();
/**
* 关闭session
*/
session.close();
/**
* 返回查询结果
*/
return list;
}
}
import java.lang.reflect.Field;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.property.ChainedPropertyAccessor;
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.PropertyAccessorFactory;
import org.hibernate.property.Setter;
import org.hibernate.transform.ResultTransformer;
import xml.dao.admin.AdminArea;
public class SqlColumnToBean implements ResultTransformer {
private static final long serialVersionUID = 1L;
private final Class<? extends AdminArea> resultClass;
private Setter[] setters;
private PropertyAccessor propertyAccessor;
public SqlColumnToBean(Class<? extends AdminArea> resultClass) {
if (resultClass == null)
throw new IllegalArgumentException("resultClass cannot be null");
this.resultClass = resultClass;
propertyAccessor = new ChainedPropertyAccessor(new PropertyAccessor[] {
PropertyAccessorFactory.getPropertyAccessor(resultClass, null),
PropertyAccessorFactory.getPropertyAccessor("field") });
}
/**
* 结果转换时,HIBERNATE调用此方法
*/
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if (setters == null) {
/**
* 首先初始化,取得目标POJO类的所有SETTER方法
*/
setters = new Setter[aliases.length];
for (int i = 0; i < aliases.length; i++) {
String alias = aliases[i];
if (alias != null) {
/**
* 我的逻辑主要是在getSetterByColumnName方法里面,其它都是HIBERNATE的另一个类中COPY的
* 这里填充所需要的SETTER方法
*/
setters[i] = getSetterByColumnName(alias);
}
}
}
result = resultClass.newInstance();
/**
* 这里使用SETTER方法填充POJO对象
*/
for (int i = 0; i < aliases.length; i++) {
if (setters[i] != null) {
setters[i].set(result, tuple[i], null);
}
}
} catch (InstantiationException e) {
throw new HibernateException("Could not instantiate resultclass: "
+ resultClass.getName());
} catch (IllegalAccessException e) {
throw new HibernateException("Could not instantiate resultclass: "
+ resultClass.getName());
}
return result;
}
/**
* 根据数据库字段名在POJO查找JAVA属性名,参数就是数据库字段名,如:USER_ID
*
* @author quxilin
* @param alias
* @return
*/
private Setter getSetterByColumnName(String alias) {
/**
* 取得POJO所有属性名
*/
Field[] fields = resultClass.getDeclaredFields();
if (fields == null || fields.length == 0) {
throw new RuntimeException("实体" + resultClass.getName() + "不含任何属性");
}
/**
* 把字段名中所有的下杠去除
*/
String proName = alias.replaceAll("_", "").toLowerCase();
for (Field field : fields) {
if (field.getName().toLowerCase().equals(proName)) {
/**
* 去除下杠的字段名如果和属性名对得上,就取这个SETTER方法
*/
return propertyAccessor.getSetter(resultClass, field.getName());
}
}
throw new RuntimeException(
"找不到数据库字段 :"
+ alias
+ " 对应的POJO属性或其getter方法,比如数据库字段为USER_ID或USERID,那么JAVA属性应为userId");
}
@SuppressWarnings("unchecked")
public List transformList(List collection) {
return collection;
}
}