火星文 技术研习社

Noname Cat, Keep Thinking
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

求struts+hibernate分页实例..

Posted on 2006-04-23 18:55  剑廿三  阅读(286)  评论(0)    收藏  举报
以下是我封装hibernate写的分页方法
public static Collection executeQuery(
String hql,
Object[] params,
int firstResult,
int maxResults) {
try {
Session session = HibernateSessionFactory.currentSession();
Query query = session.createQuery(hql);

if (null != params && 0 < params.length) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params); //numbered from 0
}
}

if (0 < firstResult) {
query.setFirstResult(firstResult);
}

if (0 < maxResults) {
query.setMaxResults(maxResults);
}

return query.list();
} catch (HibernateException he) {
throw new RuntimeException(he);
}
} // end executeQuery

/**
*
* <p><code>queryWithNoArg</code></p>
* @param name
* @return
* @author&#65533;GZiven 2004-7-29
* @since&#65533;G1.0
*/
public static Collection queryWithNoArg(String name) {
try {
Session session = HibernateSessionFactory.currentSession();
Query query = session.getNamedQuery(name);
return query.list();
} catch (HibernateException he) {
throw new RuntimeException(he);
}
} //end queryWithNoArg

/**
*
* <p><code>queryWithArg</code></p>
* @param name
* @return
* @author&#65533;GZiven 2004-7-29
* @since&#65533;G1.0
*/
public static Collection queryWithArg(String name, Map mapParam) {
try {
Session session = HibernateSessionFactory.currentSession();
Query query = session.getNamedQuery(name);
//
String[] nameParams = query.getNamedParameters();
if (null != mapParam && 0 != nameParams.length) {
for (int i = 0; i < nameParams.length; i++) {
String paramName = nameParams;
Object paramValue = mapParam.get(paramName);
if (paramValue instanceof String) {
query.setString(paramName, (String) paramValue);
} else if (paramValue instanceof Integer) {
query.setInteger(
paramName,
((Integer) paramValue).intValue());
} else {
throw new IllegalArgumentException("Parameters does not String!");
}
}
}
return query.list();
} catch (HibernateException he) {
throw new RuntimeException(he);
}
} //end queryWithArg

/**
* <p><code>queryWithArg</code></p>
* @param name
* @return
* @author&#65533;GZiven 2004-8-2
* @since&#65533;G1.0
*/
public static Collection queryWithArg(String name, Object beanParam) {
try {
Session session = HibernateSessionFactory.currentSession();
Query query = session.getNamedQuery(name);
//
query.setProperties(beanParam);
//
return query.list();
} catch (HibernateException he) {
throw new RuntimeException(he);
}
} //end queryWithArg
给你参考