一、实体类
* * 一级分类的实体类对象 */ public class Category implements Serializable { private Integer cid; private String cname; //一级分类中存放二级分类的集合 private Set<CategorySecond> categorySeconds=new HashSet<CategorySecond>(); //get、set } /* * 二级分类实体 */ public class CategorySecond { private Integer csid; private String csname; //所属一级分类,存的是一级分类的对象 private Category category; //配置商品集合 private Set<Product> products=new HashSet<Product>(); //get、set }
二、映射文件
1、一级分类
<hibernate-mapping>
<class name="cn.itcast.shop.category.vo.Category" table="category">
<id name="cid">
<generator class="native"/>
</id>
<property name="cname"/>
<!-- 配置二级分类的集合 order-by是按顺序排列 -->
<set order-by="csid" name="categorySeconds" lazy="false">
<key column="cid"/>
<one-to-many class="cn.itcast.shop.categorysecond.vo.CategorySecond"/>
</set>
</class>
</hibernate-mapping>
2、二级分类
<hibernate-mapping>
<class name="cn.itcast.shop.categorysecond.vo.CategorySecond" table="categorysecond">
<id name="csid">
<generator class="native"/>
</id>
<property name="csname"/>
<!-- 二级分类与一级分类的关联 lazy为延迟加载 -->
<many-to-one name="category" lazy="false" class="cn.itcast.shop.category.vo.Category" column="cid"></many-to-one>
<!-- 二级分类与商品的关联 -->
<set name="products">
<key column="csid"/>
<one-to-many class="cn.itcast.shop.product.vo.Product"/>
</set>
</class>
</hibernate-mapping>
三、action
/* * 商品的Action对象 */ public class ProductAction extends ActionSupport implements ModelDriven<Product> { //用于接收数据的模型驱动 private Product product=new Product(); public Product getModel() { return product; } //注入商品的Service的方法 private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } //接收分类cid private Integer cid; //接收二级分类的id private Integer csid; //注入一级分类的Service private CategoryService categoryService; //接收当前的页数 private int page; //根据分类的id查询商品 public String findByCid(){ //List<Category> cList=categoryService.findAll(); PageBean<Product> pageBean=productService.findByPageCid(cid,page);//根据一级分类查询商品,分页查询 //将PageBean存入到值栈中 ActionContext.getContext().getValueStack().set("pageBean", pageBean); return "findByCid"; } //根据二级分类的ID查询商品 public String findByCsid(){ //根据二级分类查询商品 PageBean<Product> pageBean=productService.findByPageCsid(csid,page); //将PageBean存入到值栈中 ActionContext.getContext().getValueStack().set("pageBean", pageBean); return "findByCsid"; } }
四、分页查询类
分页类的封装 package cn.itcast.shop.utils; import java.util.List; /* * 分页类的封装 */ public class PageBean<T> { private int page;//当前页数 private int totalCount;//总记录数 private int totalPage;//总页数 private int limit;//每页显示的记录数 private List<T> 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<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
五、Service
package cn.itcast.shop.product.service; import java.util.List; import org.springframework.transaction.annotation.Transactional; import cn.itcast.shop.product.dao.ProductDao; import cn.itcast.shop.product.vo.Product; import cn.itcast.shop.utils.PageBean; /* * 商品的业务层代码 */ @Transactional public class ProductService { //注入ProductDao private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; } //首页上热门商品查询 public List<Product> findHot() { return productDao.findHot(); } //首页上最新商品的查询 public List<Product> findNew() { return productDao.findNew(); } //根据商品的ID查询商品 public Product findByPid(Integer pid) { return productDao.findByPid(pid); } //根据一级分类的cid带有分页查询商品 public PageBean<Product> findByPageCid(Integer cid, int page) { PageBean<Product> pageBean=new PageBean<Product>(); //设置当前页数 pageBean.setPage(page); //设置每页显示记录数 int limit=8; pageBean.setLimit(limit); //设置总记录数 int totalCount=0; totalCount=productDao.findCountCid(cid); pageBean.setTotalCount(totalCount); //设置总页数 int totalPage=0; //Math.ceil(totalCount / limit); if(totalCount % limit==0){ totalPage=totalCount/limit; }else{ totalPage=totalCount/limit + 1; } pageBean.setTotalPage(totalPage); //每页显示的数据集合 //从哪开始 int begin=(page-1)*limit; List<Product> list=productDao.findByPageCid(cid,begin,limit); pageBean.setList(list); return pageBean; } //根据二级分类查寻商品信息 public PageBean<Product> findByPageCsid(Integer csid, int page) { PageBean<Product> pageBean=new PageBean<Product>(); //设置当前页数 pageBean.setPage(page); //设置每页显示记录数 int limit=8; pageBean.setLimit(limit); //设置总记录数 int totalCount=0; totalCount=productDao.findCountCsid(csid); pageBean.setTotalCount(totalCount); //设置总页数 int totalPage=0; //Math.ceil(totalCount / limit); if(totalCount % limit==0){ totalPage=totalCount/limit; }else{ totalPage=totalCount/limit + 1; } pageBean.setTotalPage(totalPage); //每页显示的数据集合 //从哪开始 int begin=(page-1)*limit; List<Product> list=productDao.findByPageCsid(csid,begin,limit); pageBean.setList(list); return pageBean; } }
六、DAO
package cn.itcast.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.itcast.shop.product.vo.Product; import cn.itcast.shop.utils.PageHibernateCallback; /* * 商品的持久层代码 */ public class ProductDao extends HibernateDaoSupport{ //根据分类id查询商品的个数 public int findCountCid(Integer cid) { String hql="select count(*) from Product p where p.categorySecond.category.cid=?"; List<Long> list=this.getHibernateTemplate().find(hql,cid); if(list != null && list.size()>0){ return list.get(0).intValue(); } return 0; } //根据分类id查询商品的集合 public List<Product> findByPageCid(Integer cid, int begin, int limit) { String hql="select p from Product p join p.categorySecond cs join cs.category c where c.cid=?"; //分页另一种写法 List<Product> list=this.getHibernateTemplate().execute(new PageHibernateCallback<Product>(hql,new Object[]{cid},begin,limit)); if(list != null && list.size()>0){ return list; } return null; } //根据二级分类查询商品个数 public int findCountCsid(Integer csid) { String hql="select count(*) from Product p where p.categorySecond.csid=?"; List<Long> list=this.getHibernateTemplate().find(hql, csid); if(list != null && list.size()>0){ return list.get(0).intValue(); } return 0; } //根据二级分类查询商品信息 public List<Product> findByPageCsid(Integer csid, int begin, int limit) { String hql="select p from Product p join p.categorySecond cs where cs.csid=?"; List<Product> list=this.getHibernateTemplate().execute(new PageHibernateCallback<Product>(hql,new Object[]{csid},begin,limit)); if(list != null && list.size()>0){ return list; } return null; } }
七、JSP
<div class="pagination"> <span>第 <s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/> 页</span> <s:if test="cid != null"> <s:if test="pageBean.page != 1"> <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=1" class="firstPage"> </a> <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage"> </a> </s:if> <s:iterator var="i" begin="1" end="pageBean.totalPage"> <s:if test="pageBean.page != #i"> <a href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a> </s:if> <s:else> <span class="currentPage"><s:property value="#i"/></span> </s:else> </s:iterator> <s:if test="pageBean.page != pageBean.totalPage"> <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page+1"/>"> </a> <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.totalPage"/>"> </a> </s:if> </s:if> <s:if test="csid != null"> <s:if test="pageBean.page != 1"> <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=1" class="firstPage"> </a> <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage"> </a> </s:if> <s:iterator var="i" begin="1" end="pageBean.totalPage"> <s:if test="pageBean.page != #i"> <a href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a> </s:if> <s:else> <span class="currentPage"><s:property value="#i"/></span> </s:else> </s:iterator> <s:if test="pageBean.page != pageBean.totalPage"> <a class="nextPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.page+1"/>"> </a> <a class="lastPage" href="${ pageContext.request.contextPath }/product_findByCsid.action?csid=<s:property value="csid"/>&page=<s:property value="pageBean.totalPage"/>"> </a> </s:if> </s:if> </div>
天地大矣,我心辽矣;恰同学少年,来日方长!
浙公网安备 33010602011771号