前面说过会把小学期实践所写代码附上,今天主要就是解析这些代码,水平有限,多多包涵。前面也说过一点,SSH框架由struts+spring+hibernate组成,当然我们要完成一个完整的工程,还必须把它和前后端的内容联系起来,下面就以小学期做的项目做背景。我们小学期做的项目是一个公司员工信息的维护环境,主要实现的功能是:添加员工信息、删除员工信息、修改员工信息、查询员工信息、按条件查询员工信息。

1、Spring:其实SSH并不只是一个框架,它是多个框架的集合,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

ssh项目技术架构图 ssh项目结构_xml

下面看看在项目中搭好Spring框架后的包

ssh项目技术架构图 ssh项目结构_xml_02

2、Hibernate:它是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。下面看看它的接口和在项目中实现后的包

ssh项目技术架构图 ssh项目结构_struts_03

ssh项目技术架构图 ssh项目结构_xml_04

 

ssh项目技术架构图 ssh项目结构_struts_05

下面是Hibernate在项目中与后端的连接代码

<?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="com.crm.bean.Cust" table="cust">
		<id name="id" type="java.lang.Integer" column="id">
			<generator class="increment"></generator>
		</id>
		<property name="custno" type="string" column="custno" length="20"/>
		<property name="custname" type="string" column="custname" length="80"/>
	    <property name="telephone" type="string" column="telephone" length="15"/>
		<!--<property name="age" type="int" column="age"/>		
		<property name="position" type="string" column="position" length="80"/>
		<property name="logindate" type="string" column="logindate" length="10"/>-->
	</class>
</hibernate-mapping>

3、struts:它 是MVC的一种实现,它将 Servlet和 JSP 标记(属于 J2EE 规范)用作实现的一部分。Struts继承了MVC的各项特性,并根据J2EE的特点,做了相应的变化与扩展,减弱了业务逻辑接口和数据接口之间的耦合,以及让视图层更富于变化,另外, struts具有页面导航功能,使系统的脉络更加清晰。通过一个配置文件,即可把握整个系统各部分之间的联系,这对于后期的维护有着莫大的好处。尤其是当另一批开发者接手这个项目时,这种优势体现得更加明显。

下面看看struts在项目中的配置文件源码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="cust" extends="struts-default">
		<!-- 保存 -->
		<action name="saveCust" class="custSaveAction">
			<result name="success" type="redirect">/jsp/custInfo.jsp</result>
			<result name="input">/custSave.jsp</result>
		</action>
		<!-- 查询 -->
		<action name="listCust" class="custListAction">
			<result>/jsp/custInfo.jsp</result>
		</action>
		<!-- 删除 -->
		<action name="removeCust" class="custRemoveAction">
			<result>/jsp/custInfo.jsp</result>
		</action>
		<!-- 条件查询 -->
        <action name="findCdtCustList" class="custFindAction">
	    <result>/jsp/custInfo.jsp</result>
        </action>
     <!-- 修改 -->
     <action name="updateCust" class="updateCustAction">
    <result name="success" type="redirect">listCustomer.action</result>
	<result name="input">/jsp/custUpdate.jsp</result>
     </action>
     <!-- 修改预览 -->
    <action name="updatePreviewCust" class="updatePreviewCustAction">
	<result name="success">/jsp/custUpdate.jsp</result>
    </action>
	</package>
</struts>4、 ApplicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	                    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">   <bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/dbssh">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
		<property name="maxActive" value="100"></property>
		<property name="maxWait" value="500"></property>
		<property name="defaultAutoCommit" value="true"></property>
	</bean>
	<!--sessionFactory配置与管理  -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/crm/bean/Cust.hbm.xml</value>
			</list>
		</property>
	</bean>
	<!-- 配置 -->
     <bean id="dao" class="com.crm.impl.CustDaoImpl">
     <property name="sessionFactory">
     <ref bean="sessionFactory"/>
     </property>
     </bean>
     <!-- 配置service -->
     <bean id="custService" class="com.crm.service.impl.CustServiceImpl">
		<property name="custDao" ref="dao"></property>
	</bean>
     <!-- 配置-saveaction -->
	<bean id="custSaveAction" class="com.crm.action.CustSaveAction">
	   <property name="custService" ref="custService"></property>
	</bean>
	<!--配置-查询listAction  -->
	<bean id="custListAction" class="com.crm.action.CustListAction">
		<property name="custService" ref="custService"></property>
	</bean>
		<!--配置-删除deleteAction  -->
	<bean id="custRemoveAction" class="com.crm.action.CustRemoveAction">
		<property name="custService" ref="custService"></property>
	</bean>
	<!--配置-条件查询findCdtAction  -->
	<bean id="custFindAction" class="com.crm.action.CustFindByConditionAction">
		<property name="custService" ref="custService"></property>
	</bean>
	<!--配置-修改custUpdateAction  -->
	<bean id="updateCustAction" class="com.crm.action.UpdateCustAction">
		<property name="updateCustService" ref="custService"></property>
	</bean>
	<!--配置-修改预览updatePreviewAction  -->
	<bean id="updatePreviewCustAction" class="com.crm.action.UpdatePreviewCustAction">
		<property name="updatePreviewCustService" ref="custService"></property>
	</bean>
</beans>