package com.szy.bean.daoImpl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.szy.bean.dao.ProductDao;
import com.szy.bean.pojo.Product;
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao{
/**
* 查找前八条记录的产品信息(在Struts2的action中定义pageNow为0,pageSize为8)
*/
@SuppressWarnings("unchecked")
public Object doInHibernate(Session session) throws HibernateException,SQLException {
//查询前八条记录
Query query=session.createQuery(hql);
query.setFirstResult(pageNow);
query.setMaxResults(pageSize);
return query.list();
}
});
return products;
}
}
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.szy.bean.dao.ProductDao;
import com.szy.bean.pojo.Product;
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao{
/**
* 查找前八条记录的产品信息(在Struts2的action中定义pageNow为0,pageSize为8)
*/
@SuppressWarnings("unchecked")
public List<Product> findAllProduct(int pageNow , int pageSize) {
//创建集合对象,用于存放产品信息
List<Product> products=new ArrayList<Product>();
//Hibernate的HQL语言
final String hql="from Product p order by p.id";
//根据HibernateTemplate类的execute()方法查找记录
products=(List<Product>)this.getHibernateTemplate().execute(new HibernateCallback() {public Object doInHibernate(Session session) throws HibernateException,SQLException {
//查询前八条记录
Query query=session.createQuery(hql);
query.setFirstResult(pageNow);
query.setMaxResults(pageSize);
return query.list();
}
});
return products;
}
}