做
Spring+Hibernate
分页处理要使用到一个接口
org.springframework.orm.hibernate3.HibernateCallback
该接口中有一个函数
Object doInHibernate (Session session)
我从文档上理解所得的意思为
HibernateTemplate.execute
执行中调用的
session,
现在可以根据这个
session
进行操作,该资料可以在
Spring
官方网站上查询
www.springframework.org
另外还有一个
Query
接口,它有两个函数,
setFirstResult
(int firstResult)
和
setMaxResults
(int maxResults)
,这两个函数看名取意,就不多说了,下面来个例子
接口
package
com.david.dao;
import
org.hibernate.type.Type;
import
org.springframework.orm.hibernate3.HibernateCallback;
public
interface IPageBeanSH extends HibernateCallback {
public Type[] getArrTypes();
public void setArrTypes(Type[] arrTypes);
public Object[] getArrValues();
public void setArrValues(Object[] arrValues);
public String getHql();
public void setHql(String hql);
public int getNum();
public void setNum(int num);
public int getStart();
public void setStart(int start);
}
实现类
package com.david.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
/**
*
负责分页的
bean
*
* @author Administrator
*
*/
public class PageBeanSH implements IPageBeanSH {
//
起始位置
private int start=0;
//
数量
private int num;
//
查询语句
private String hql;
//
对象数组
,
查询
private Object[] arrValues;
//
对象
private Type[] arrTypes;
public PageBeanSH() {
super();
// TODO Auto-generated constructor stub
}
public PageBeanSH(String hql,Object[] arrValues) {
this.hql = hql;
this.arrValues=arrValues;
// TODO Auto-generated constructor stub
}
/**
*
构造函数
* @param start
* @param num
* @param hql
*/
public PageBeanSH(int start, int num, String hql,Object[] arrValues,Type[] arrTypes) {
super();
this.start = start;
this.num = num;
this.hql = hql;
this.arrValues=arrValues;
this.arrTypes=arrTypes;
}
/**
*
得出查询结果
*/
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
Query query=session.createQuery(hql);
query.setFirstResult(start*num);
query.setMaxResults(num);
//
循环套入参数
if(arrValues.length==0){
;
}else{
for(int i=0;i<arrValues.length;i++){
query.setParameter(i, arrValues[i]);
}
}
return query.list();
}
public Type[] getArrTypes() {
return arrTypes;
}
public void setArrTypes(Type[] arrTypes) {
this.arrTypes = arrTypes;
}
public Object[] getArrValues() {
return arrValues;
}
public void setArrValues(Object[] arrValues) {
this.arrValues = arrValues;
}
public String getHql() {
return hql;
}
public void setHql(String hql) {
this.hql = hql;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
}
这样就可以通过设置
hql
语句和参数进行分页操作
例如
package com.david.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.david.bo.Category;
import com.david.bo.Userinfo;
public class CategoryDao extends HibernateDaoSupport implements ICategoryDao {
private IPageBeanSH pageBeanSH;
public boolean addCategory(Category category) {
// TODO Auto-generated method stub
try{
this.getHibernateTemplate().save(category);
return true;
}catch(DataAccessException e){
e.printStackTrace();
return false;
}
}
public boolean deleteCategory(Category category) {
// TODO Auto-generated method stub
return false;
}
public List<Category> getCategories(int start, Userinfo userinfo) {
// TODO Auto-generated method stub
String hql="from Category as category where category.userinfo=?";// as c where c.userinfo=?";
pageBeanSH.setHql(hql);
pageBeanSH.setStart(start);
Userinfo[] arrValues={userinfo};
pageBeanSH.setArrValues(arrValues);
try {
return (List<Category>) pageBeanSH.doInHibernate(this.getSession());
} catch (DataAccessResourceFailureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public boolean updateCategory(Category category) {
// TODO Auto-generated method stub
return false;
}
public void setPageBeanSH(IPageBeanSH pageBeanSH) {
// TODO Auto-generated method stub
this.pageBeanSH=pageBeanSH;
}
public int getCategoriesNum(Userinfo userinfo) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find("from Category as category where category.userinfo=?", userinfo).size();
}
}
在函数
public List<Category> getCategories(int start, Userinfo userinfo)
通过设置起始位置,
hsl
语句,参数列表数组就可以进行分页的查询操作了