码迷,mamicode.com
首页 > 其他好文 > 详细

13.首页热门商品显示

时间:2015-05-03 13:20:58      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

1、建表

创建二级分类的表:
CREATE TABLE `categorysecond` (
  `csid` int(11) NOT NULL AUTO_INCREMENT,
  `csname` varchar(255) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL,
  PRIMARY KEY (`csid`),
  KEY `FK936FCAF21DB1FD15` (`cid`),
  CONSTRAINT `FK936FCAF21DB1FD15` FOREIGN KEY (`cid`) REFERENCES `category` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;
创建商品表:
CREATE TABLE `product` (
  `pid` int(11) NOT NULL AUTO_INCREMENT,
  `pname` varchar(255) DEFAULT NULL,
  `market_price` double DEFAULT NULL,
  `shop_price` double DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL,
  `num` int(11) DEFAULT NULL,
  `pdesc` varchar(255) DEFAULT NULL,
  `is_hot` int(11) DEFAULT NULL,
  `pdate` datetime DEFAULT NULL,
  `csid` int(11) DEFAULT NULL,
  PRIMARY KEY (`pid`),
  KEY `FKED8DCCEFB9B74E02` (`csid`),
  CONSTRAINT `FKED8DCCEFB9B74E02` FOREIGN KEY (`csid`) REFERENCES `categorysecond` (`csid`)
) ENGINE=InnoDB AUTO_INCREMENT=74 DEFAULT CHARSET=utf8;

2、创建包及类

Product.java

package cn.xdy.shop.product.vo;

import java.util.Date;

public class Product {
	private Integer pid;
	private String pname;
	private Double market_price;
	private Double shop_price;
	private String image;
	private String pdesc;
	private Integer is_hot;
	private Date pdate;

	// 二级分类ID
	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getPdesc() {
		return pdesc;
	}

	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}

	public Double getMarket_price() {
		return market_price;
	}

	public void setMarket_price(Double market_price) {
		this.market_price = market_price;
	}

	public Double getShop_price() {
		return shop_price;
	}

	public void setShop_price(Double shop_price) {
		this.shop_price = shop_price;
	}

	public Integer getIs_hot() {
		return is_hot;
	}

	public void setIs_hot(Integer is_hot) {
		this.is_hot = is_hot;
	}

	public Date getPdate() {
		return pdate;
	}

	public void setPdate(Date pdate) {
		this.pdate = pdate;
	}

}

Product.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="cn.xdy.shop.product.vo.Product" table="product">
		<id name="pid">
			<generator class="native"/>
		</id>
		<property name="pname"/>
		<property name="market_price"/>
		<property name="shop_price"/>
		<property name="image"/>
		<property name="pdesc"/>
		<property name="is_hot"/>
		<property name="pdate"/>
	</class>
</hibernate-mapping>

将Product.hbm.xml加入到applicationContext.xml中

<!-- 配置Hibernate的映射文件 -->
		<property name="mappingResources">
			<list>
				<value>cn/xdy/shop/user/vo/User.hbm.xml</value>
				<value>cn/xdy/shop/category/vo/Category.hbm.xml</value>
				<value>cn/xdy/shop/product/vo/Product.hbm.xml</value>
			</list>
		</property>

IndexAction.java中查询热门商品,将查询到的数据保存到值栈中。

/**
	 * 执行访问首页的方法
	 */
	public String execute() throws Exception {
		// 首页一级分类的显示
		List<Category> cList = categoryService.findAll();
		ActionContext.getContext().getSession().put("cList", cList);

		// 首页热门商品显示
		List<Product> hlist = productService.findHot();
		ActionContext.getContext().getValueStack().set("hlist", hlist);
		return "index";
	}

ProductService.java

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;

@Transactional
public class ProductService {
	private ProductDao productDao;

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public List<Product> findHot() {
		return productDao.findHot();
	}
	
	
}

ProductDao.java

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;

public class ProductDao extends HibernateDaoSupport{

	public List<Product> findHot() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		criteria.add(Restrictions.eq("is_hot", 1));
		criteria.addOrder(Order.desc("pdate"));
		List<Product> list = this.getHibernateTemplate().findByCriteria(criteria, 0, 10);
		return list;
	}

}

完成Service和Dao的配置

<bean id="productDao" class="cn.xdy.shop.product.dao.ProductDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

<bean id="productService" class="cn.xdy.shop.product.service.ProductService">
		<property name="productDao" ref="productDao"></property>
	</bean>

indexAction中注入productService

<!-- 首页访问的action -->
	<bean id="indexAction" class="cn.xdy.shop.index.action.IndexAction" scope="prototype">
		<property name="categoryService" ref="categoryService"></property>
		<property name="productService" ref="productService"></property>
	</bean>

3、效果图

技术分享

13.首页热门商品显示

标签:

原文地址:http://blog.csdn.net/xiongwt/article/details/45458181

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!