16.首页查询一级分类商品(分类显示商品)

1、封装分页PageBean

package cn.xdy.shop.util;

import java.util.List;

public class PageBean {
	private int page;//当前页数
	private int totalCount;//总记录数
	private int totalPage;//总页数
	private int limit;//每页显示的记录数
	private List list;//每页显示数据的集合
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	
	
}

2、menu.jsp


					
  • &page=1">|

  • 3、ProductAction

    package cn.xdy.shop.product.action;
    
    import cn.xdy.shop.product.service.ProductService;
    import cn.xdy.shop.product.vo.Product;
    import cn.xdy.shop.util.PageBean;
    
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    
    public class ProductAction extends ActionSupport implements ModelDriven{
    	private ProductService productService;
    	private Product product = new Product();
    	private int page;//当前页数
    	private int cid;//接收分类cid
    	
    	
    	public int getCid() {
    		return cid;
    	}
    
    	public void setCid(int cid) {
    		this.cid = cid;
    	}
    
    	public void setPage(int page) {
    		this.page = page;
    	}
    
    	public void setProductService(ProductService productService) {
    		this.productService = productService;
    	}
    
    	public Product getModel() {
    		return product;
    	}
    	
    	public String findByPid(){
    		product = productService.findByPid(product.getPid());
    		return "findByPid";
    	}
    	
    	/**
    	 * 根据分类ID查询所有
    	 * @return
    	 */
    	
    	public String findByCid(){
    		//因为IndexAction中查询了一级分类存入session中,所以此处不再查询,页面上直接从session中取即可。
    		PageBean pageBean = productService.findByPageCid(cid,page);
    		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
    		return "findByCid";
    	}
    	
    	
    }
     

    4、ProductService

    package cn.xdy.shop.product.service;
    
    import java.util.List;
    
    import org.springframework.transaction.annotation.Transactional;
    
    import cn.xdy.shop.product.dao.ProductDao;
    import cn.xdy.shop.product.vo.Product;
    import cn.xdy.shop.util.PageBean;
    
    @Transactional
    public class ProductService {
    	private ProductDao productDao;
    
    	public void setProductDao(ProductDao productDao) {
    		this.productDao = productDao;
    	}
    
    	/**
    	 * 查询最热门商品
    	 * @return
    	 */
    	public List findHot() {
    		return productDao.findHot();
    	}
    
    	/**
    	 * 查询最新商品
    	 * @return
    	 */
    	public List findNew() {
    		return productDao.findNew();
    	}
    
    	/**
    	 * 根据pid查询商品信息
    	 * @param pid
    	 * @return
    	 */
    	public Product findByPid(Integer pid) {
    		return productDao.findByPid(pid);
    	}
    
    	/**
    	 * 根据一级分类的cid带有分页查询商品
    	 * @param cid
    	 * @param page
    	 * @return
    	 */
    	public PageBean findByPageCid(int cid, int page) {
    		PageBean pageBean = new PageBean();
    		//设置当前页数
    		pageBean.setPage(page);
    		//设置每页显示记录数
    		int limit = 16;
    		pageBean.setLimit(limit);
    		//设置总记录数
    		int totalCount = 0;
    		totalCount = productDao.findCountCid(cid);
    		pageBean.setTotalCount(totalCount);
    		//设置总页数
    		int totalPage = 0;
    		if(totalCount%limit==0){
    			totalPage = totalCount/limit;
    		}else{
    			totalPage = totalCount/limit+1;
    		}
    		pageBean.setTotalPage(totalPage);
    		//每页显示的数据集合
    		int begin = (page-1)*limit;
    		List list = productDao.findByPageCid(cid,begin,limit);
    		pageBean.setList(list);
    		return pageBean;
    	}
    	
    	
    }
    

    5、ProductDao

    package cn.xdy.shop.product.dao;
    
    import java.util.List;
    
    import org.hibernate.criterion.DetachedCriteria;
    import org.hibernate.criterion.Order;
    import org.hibernate.criterion.Restrictions;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import cn.xdy.shop.product.vo.Product;
    import cn.xdy.shop.util.PageHibernateCallback;
    
    public class ProductDao extends HibernateDaoSupport{
    
    	/**
    	 * 查询最热门商品
    	 * @return
    	 */
    	public List findHot() {
    		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
    		criteria.add(Restrictions.eq("is_hot", 1));
    		criteria.addOrder(Order.desc("pdate"));
    		List list = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
    		return list;
    	}
    
    	/**
    	 * 查询最新商品
    	 * @return
    	 */
    	public List findNew() {
    		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
    		criteria.addOrder(Order.desc("pdate"));
    		List list = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
    		return list;
    	}
    
    	/**
    	 * 根据ID查询商品
    	 * @param pid
    	 * @return
    	 */
    	public Product findByPid(Integer pid) {
    		return this.getHibernateTemplate().get(Product.class, pid);
    	}
    
    	/**
    	 * 根据分类ID查询个数
    	 * @param cid
    	 * @return
    	 */
    	public int findCountCid(int cid) {
    		String hql = "select count(*) from Product p where p.categorySecond.category.cid=?";
    		List list = this.getHibernateTemplate().find(hql, cid);
    		if(list!=null && list.size()>0){
    			return list.get(0).intValue();
    		}
    		return 0;
    	}
    
    	/**
    	 * 根据分类ID查询带有分页的商品的集合
    	 * @param cid
    	 * @param begin
    	 * @param limit
    	 * @return
    	 */
    	public List findByPageCid(int cid, int begin, int limit) {
    		String hql = "select p from Product p join p.categorySecond cs join cs.category c where c.cid=?";
    		List list = this.getHibernateTemplate().execute(new PageHibernateCallback(hql, new Object[]{cid}, begin, limit));
    		return list;
    	}
    
    }
    

    6、ProductList.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    
    
    
    
    
    星辰网上商城
    
    
    
    
    
    
    正品保障
    <%@include file="menu.jsp" %>


    你可能感兴趣的:(商城项目)