hibernate执行原生sql


方法所在hibernate dao必须extends HibernateTemplate



public List performPSql(final String querysql, final Integer startIndex, final Integer pageSize) {
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
try {
SQLQuery query = session.createSQLQuery(querysql);
//pageSize 不为0 时做分页查询
if (pageSize.intValue() != 0) {
query.setFirstResult(startIndex.intValue()*(pageSize.intValue()));
query.setMaxResults(pageSize.intValue());
}
return query.list();
} catch (Exception e) {
e.printStackTrace();
throw new HibernateException("perform Primitive Sql error");
}
}
};
return (List)getHibernateTemplate().execute(callback);
}

你可能感兴趣的:(hibernate执行原生sql)