1.IndexAction-----ProductService------>ProductDao
* 查询最新商品:
* 按商品上传日期倒序进行排序 只显示10个
* 将查询到记录保存到值栈中:
IndexAction代码如下
/**
* 执行的访问首页的方法:
*/
public String execute(){
//查询所有一级分类
List<Category> cList = categoryService.findAll();
//将一级分类存入session范围内
ActionContext.getContext().getSession().put("cList", cList);
//查询热门商品
List<Product> hList = productService.findHot();
//保存在值栈中
ActionContext.getContext().getValueStack().set("hList", hList);
//查询最新的商品
List<Product> nList = productService.findNew();
//保存在值栈中
ActionContext.getContext().getValueStack().set("nList", nList);
return "index";
}
ProductService代码如下:
//首页上的最新商品
public List<Product> findNew() {
return productDao.findNew();
}
ProductDao代码如下:
/**
* 首页上的最新商品的查询
* @return
*/
public List<Product> findNew() {
//使用离线查询条件
DetachedCriteria criteria =DetachedCriteria.forClass(Product.class);
//倒序排序输出
criteria.addOrder(Order.desc("pdate"));
//执行查询,分页
List<Product> nlist = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
return nlist;
}
效果前后对比图如下:更改之前
更改之后的图片