SSH框架流程

本文分享了一个月学习成果,通过实战项目网上商城的搭建过程,深入浅出地介绍了SSH框架在Java中的应用,涵盖实体类定义、映射文件配置、三层架构设计等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    经过一个月的学习,终于将网上商城敲出来了,让我对SSH框架在Java中的应用有了一个初步的了解,一条线的流程也会了。但现在还只是在会的程度上,有好的的东西其实并没有理解,希望在接下来的学习中进一步学习。。


    流程图


    具体步骤

    一、实体类

//Serializable在网络的环境下做类传输
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"/>
        
        <!-- 配置二级分类的集合 lazy是不进行延迟加载;cascade可以级联删除,one一方删除一个,many一方全部删除-->
        <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>    

    三、对三层进行编写

    1Action

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;
    }
}

    2Service

//事务配置
@Transactional
public class CategoryService {
    //注入CategoryDao
    private CategoryDao categoryDao;

    public void setCategoryDao(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }
}

    3Dao

public class CategoryDao extends HibernateDaoSupport {
    //Dao层的查询所有一级分类的方法
    public List<Category> findAll() {
        String hql="from Category";
        List<Category> list=this.getHibernateTemplate().find(hql);
        return list;
    }
}

    四、将三层配置到Spring

 <!-- action的配置 -->
  <bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype">
    <property name="categoryService" ref="categoryService"/>
  </bean>


<!-- 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>

    五、将数据显示到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>

    六、将Action配置到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>
        
    </package> 
</struts>

    总结

    网上商城项目不仅让我学到了SSH框架的应用,还让我对以前的知识进行了回顾,让我收获很多。在这期间遇到了很多错误,花费了很长时间,但几乎都是自己不细心造成的。。所以,我在想是应该庆幸,遇到了这么多的错误,让自己提升了很多;还是应该警醒,细节决定成败!

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值