标签:
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;
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; } }
<?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>
<!-- 配置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>
/** * 执行访问首页的方法 */ 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"; }
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(); } }
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; } }
<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>
<!-- 首页访问的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>
标签:
原文地址:http://blog.csdn.net/xiongwt/article/details/45458181