以下是我封装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�GZiven 2004-7-29
* @since�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�GZiven 2004-7-29
* @since�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�GZiven 2004-8-2
* @since�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
给你参考
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�GZiven 2004-7-29
* @since�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�GZiven 2004-7-29
* @since�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�GZiven 2004-8-2
* @since�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
给你参考