流程图
具体步骤
一、实体类
public class Category implements Serializable { private Integer cid; private String cname; //一级分类中存放二级分类的集合 private Set<CategorySecond> categorySeconds=new HashSet<CategorySecond>(); public Set<CategorySecond> getCategorySeconds() { return categorySeconds; } public void setCategorySeconds(Set<CategorySecond> categorySeconds) { this.categorySeconds = categorySeconds; } public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
二、映射文件
1、代码
<?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.itcast.shop.category.vo.Category" table="category"> <id name="cid"> <generator class="native"/> </id> <property name="cname"/> <!-- 配置二级分类的集合 --> <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、配置到spring
<!--Hibernate的相关信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置Hibernate的其他的属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.connection.autocommit">false</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 配置Hibernate的映射文件 --> <property name="mappingResources"> <list> <value>cn/itcast/shop/category/vo/Category.hbm.xml</value> </list> </property> </bean>
三、对Service和Dao进行配置
1、Service
@Transactional public class CategoryService { //注入CategoryDao private CategoryDao categoryDao; public void setCategoryDao(CategoryDao categoryDao) { this.categoryDao = categoryDao; } }
2、Dao
public class CategoryDao extends HibernateDaoSupport { //Dao层的查询所有一级分类的方法 public List<Category> findAll() { String hql="from Category"; List<Category> list=this.getHibernateTemplate().find(hql); return list; } }
四、将Service和Dao配置到spring
<!-- Service配置====================================================== --> <bean id="categoryService" class="cn.itcast.shop.category.service.CategoryService"> <property name="categoryDao" ref="categoryDao"/> </bean> <!-- Dao配置============================================================= --> <bean id="categoryDao" class="cn.itcast.shop.category.dao.CategoryDao"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
五、在Action中注入Service
public class IndexAction extends ActionSupport { //注入一级分类的service private CategoryService categoryService; public void setCategoryService(CategoryService categoryService) { this.categoryService = categoryService; } //注入商品的Service private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } /* * 执行的访问首页的方法 */ 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); return "index"; } }
六、将数据显示到jsp页面
<%@ taglib uri="/struts-tags" prefix="s" %> <div class="span24"> <ul class="mainNav"> <li> <a href="${ pageContext.request.contextPath }/index.action">首页</a> | </li> <!-- 如果保存在session中 --> <s:iterator var="c" value="#session.cList"> <!-- 如果保存在值栈中 --> <!-- <s:iterator var="p" value="nList"> --> <li> <a href="${pageContext.request.contextPath }/product_findByCid.action?cid=<s:property value="#c.cid"/>&page=1"><s:property value="#c.cname"/></a> | </li> </s:iterator> </ul> </div>
七、配置到Struts中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="false" /> <package name="shop" extends="struts-default" namespace="/"> <!-- 全局页面 --> <global-results> <result name="msg">/WEB-INF/jsp/msg.jsp</result> </global-results> <!-- 配置首页访问的Action --> <action name="index" class="indexAction"> <result name="index">/WEB-INF/jsp/index.jsp</result> </action> <!-- 验证码Action --> <action name="checkImg" class="checkImgAction"></action> </package> </struts>
时间: 2024-12-14 11:43:48