/*** 使用hql 语句进行操作
* @param hql HSQL 查询语句
* @param offset 开始取数据的下标
* @param length 读取数据记录数
* @return List 结果集
*/
public List getListForPage(final String hql, final int offset, final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list; }});
return list;}
=======================================
Spring 整合 Hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate的回调机制扩展 hibernateTemplate的功能实现分页
/**
* 使用hql 语句进行操作
* @param hql
* @param offset
* @param length
* @return List
*/
public List getListForPage(final String hql, final int offset,
final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}/**
* 使用criterion进行操作
* @param arg
* @param criterions
* @param offset
* @param length
* @return List
*/
protected List getListForPage(final Class arg, final Criterion[] criterions,final int offset, final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(arg);
//循环遍历添加约束条件
for (int i = 0; i < criterions.length; i++) {
criteria.add(criterions[i]);
}
criteria.setFirstResult(offset);
criteria.setMaxResults(length);
return criteria.list();
}
});
return list;
}
==================================================================
扩展HibernateTemplate,实现分页查询 (回调)
在使用hibernate时,可以用query.setMaxResults(10);
query.setFirstResult(130);方便的实现分页,不过spring的HibernateTemplate没有这个功能,可以用回调实现.public List findUsers(final int firstResult, final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {
String q = "from User as user";
Query query = s.createQuery(q);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
List list = query.list();
return list;
}
});
}
/**
* 使用hql 语句进行操作
* @param hql
* @param offset
* @param length
* @return List
*/
public List getListForPage(final String hql, final int offset,
final int length) {List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}/**
* 使用criterion进行操作
* @param arg
* @param criterions
* @param offset
* @param length
* @return List
*/
protected List getListForPage(final Class arg, final Criterion[] criterions,final int offset, final int length) {List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(arg);
//循环遍历添加约束条件
for (int i = 0; i < criterions.length; i++) {
criteria.add(criterions);
}
criteria.setFirstResult(offset);
criteria.setMaxResults(length);
return criteria.list();
}
});
return list;
}
============================================
hxzon
public List getCategorys(final int currentpage, final int pagesize) {
log.debug("finding all ShbbsCategory instances");
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from ShbbsCategory";
Query query = session.createQuery(hql);
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
return list;
}
public int getCount() {
int count = 0;
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "SELECT COUNT(p.id) from ShbbsCategory AS p ";
Query query = session.createQuery(hql);
List list = query.list();
return list;
}
});
if (list.size() > 0) {
count = (Integer) list.get(0);
}
return count;
}
public List getCategorysByLike(final String cond, final int currentpage,
final int pagesize) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "FROM ShbbsCategory AS p WHERE p.uid LIKE ? OR p.name LIKE ?";
Query query = session.createQuery(hql);
query.setString(0, "%" + cond + "%");
query.setString(1, "%" + cond + "%");
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
return list;
}
public int getByLikeCount(final String cond) {
int count = 0;
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "SELECT COUNT(p.id) from Person AS p WHERE p.uid LIKE ? OR p.name LIKE ?";
Query query = session.createQuery(hql);
query.setString(0, "%" + cond + "%");
query.setString(1, "%" + cond + "%");
List list = query.list();
return list;
}
});
if (list.size() > 0) {
count = (Integer) list.get(0);
}
return count;
}
原来使用getHibernateTemplate().getSessionFactory().getSession()来取得session运行期会出错。
* @param hql HSQL 查询语句
* @param offset 开始取数据的下标
* @param length 读取数据记录数
* @return List 结果集
*/
public List getListForPage(final String hql, final int offset, final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list; }});
return list;}
=======================================
Spring 整合 Hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate的回调机制扩展 hibernateTemplate的功能实现分页
/**
* 使用hql 语句进行操作
* @param hql
* @param offset
* @param length
* @return List
*/
public List getListForPage(final String hql, final int offset,
final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}/**
* 使用criterion进行操作
* @param arg
* @param criterions
* @param offset
* @param length
* @return List
*/
protected List getListForPage(final Class arg, final Criterion[] criterions,final int offset, final int length) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(arg);
//循环遍历添加约束条件
for (int i = 0; i < criterions.length; i++) {
criteria.add(criterions[i]);
}
criteria.setFirstResult(offset);
criteria.setMaxResults(length);
return criteria.list();
}
});
return list;
}
==================================================================
扩展HibernateTemplate,实现分页查询 (回调)
在使用hibernate时,可以用query.setMaxResults(10);
query.setFirstResult(130);方便的实现分页,不过spring的HibernateTemplate没有这个功能,可以用回调实现.public List findUsers(final int firstResult, final int maxResults) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s)
throws HibernateException, SQLException {
String q = "from User as user";
Query query = s.createQuery(q);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
List list = query.list();
return list;
}
});
}
/**
* 使用hql 语句进行操作
* @param hql
* @param offset
* @param length
* @return List
*/
public List getListForPage(final String hql, final int offset,
final int length) {List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
});
return list;
}/**
* 使用criterion进行操作
* @param arg
* @param criterions
* @param offset
* @param length
* @return List
*/
protected List getListForPage(final Class arg, final Criterion[] criterions,final int offset, final int length) {List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(arg);
//循环遍历添加约束条件
for (int i = 0; i < criterions.length; i++) {
criteria.add(criterions);
}
criteria.setFirstResult(offset);
criteria.setMaxResults(length);
return criteria.list();
}
});
return list;
}
============================================
hxzon
public List getCategorys(final int currentpage, final int pagesize) {
log.debug("finding all ShbbsCategory instances");
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from ShbbsCategory";
Query query = session.createQuery(hql);
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
return list;
}
public int getCount() {
int count = 0;
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "SELECT COUNT(p.id) from ShbbsCategory AS p ";
Query query = session.createQuery(hql);
List list = query.list();
return list;
}
});
if (list.size() > 0) {
count = (Integer) list.get(0);
}
return count;
}
public List getCategorysByLike(final String cond, final int currentpage,
final int pagesize) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "FROM ShbbsCategory AS p WHERE p.uid LIKE ? OR p.name LIKE ?";
Query query = session.createQuery(hql);
query.setString(0, "%" + cond + "%");
query.setString(1, "%" + cond + "%");
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
return list;
}
public int getByLikeCount(final String cond) {
int count = 0;
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "SELECT COUNT(p.id) from Person AS p WHERE p.uid LIKE ? OR p.name LIKE ?";
Query query = session.createQuery(hql);
query.setString(0, "%" + cond + "%");
query.setString(1, "%" + cond + "%");
List list = query.list();
return list;
}
});
if (list.size() > 0) {
count = (Integer) list.get(0);
}
return count;
}
原来使用getHibernateTemplate().getSessionFactory().getSession()来取得session运行期会出错。