J2EE SSH框架整合教程

本文仅作为学习和研究的参考,与实际项目使用技术有所不同,由于作者水平有限,错误疏漏在所难免,请各位看官批评指教。

项目的源代码放在:https://github.com/Frank-Pei/SSHIntegrationDemo

使用软件环境概述:JDK1.7.0_79, Eclipse Mars4.5, Oracle11gR2版本11.2.0.3, Tomcat7.0.65, Maven2

SSH框架其实是指Spring, Struts, Hibernate, SSH框架的整合其实是Spring+Struts的整合以及Spring+Hibernate的整合。

整合步骤如下:

(1) 添加三个框架的jar文件,并创建相关的配置文件。

(2) 为Hibernate添加数据源。

(3) 配置SessionFactory对象

(4) 实现并配置Dao和Service.

(5) 使用Spring简化数据库事务处理,即配置声明式事务管理。

(6) 实现并配置Action。

其中(2)-(5)属于Spring整合Hibernate,步骤(6)属于Spring整合Struts2。

(一)工程环境的搭建

1. 从创建Maven工程开始,archetype选择webapp

a1

2. Group Id & Artifact Id

a2

3. 创建完成后打开工程属性,修改Build Path里面的JRE(默认的Maven工程会使用JRE1.5)

a3

4. 点击Remove后,Add Library选择默认的jre7

a4

5. 创建完成的maven工程会报错,原因是没有将tomcat添加到Build path下,如图所示操作即可。

a5

6. 至此基础工作已经完成。开始添加工程需要的jar文件。

(二)Spring2与Hibernate3的整合

1. 在web容器中配置数据源,数据源中用到了连接池技术(Connection Pool),常用的连接池技术有c3p0, dbcp, proxool, 这里我们使用c3p0

首先,需要c3p0的jar包,可以参考本文最后的pom.xml文件;并且需要将oracle数据库jar文件放在tomcat安装目录下的lib/子目录下。

其次,配置一共要修改三个文件,分别是Tomcat安装目录下的context.xml, web.xml,spring配置文件。代码如下:

context.xml中的配置,

<Resource name="jdbc/ORCL" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@192.168.0.10:1521:ORCL"
              username="frank" password="frank" maxActive="20" maxIdle="10"
              maxWait="-1"/>

web.xml中的配置,

<resource-ref>
     <description>Oracle Datasource example</description>
     <res-ref-name>jdbc/ORCL</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
</resource-ref>

spring配置文件中的配置,

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/ORCL"></property>
</bean>

 

2. 配置SessionFactory,必须要添加spring-orm和aspectjweaver jar包

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- dataSource用于制定数据源,ref引用上面定义的ID为dataSource的Bean -->
        <property name="dataSource" ref="dataSource" />
        <!-- 指定seesionFactory查找配置文件的位置 另一种做法如下,如要制定多个配置文件必须手动添加 <property name="mappingResources"> 
            <list> <value>com/example/domain/Student.hbm.xml</value> </list> </property> -->
        <property name="mappingLocations">
            <list>
                <value>classpath:**/*.hbm.xml</value>
            </list>
        </property>
        <!-- 配置方言等属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

3. 配置Dao和Service,为做简化只配置了Dao层,没有写Service层

在Spring中配置Dao如下:

<bean id="studentDao" class="com.example.dao.StudentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

 

4. 配置声明式事务(Declarative Transaction),基于Spring的AOP特性

<!-- 事务管理器,Spring为Hibernate提供了事务管理类HibernateTransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 定义事务的AOP切入点 -->
    <aop:config>
        <aop:pointcut id="productServiceMethods" expression="execution(* com.example.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
    </aop:config>
    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

至此,Spring与Hibernate的整合完成。

 

(三)Spring2与Struts2的整合

除了spring-context, struts-core jar包外,还需要struts-spring-plugin包

1. 在web.xml中配置监听器并提供spring配置文件的位置参数

<!-- ContextLoaderListener可以在tomcat启动时初始化Spring容器,从而完成Spring的加载 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- contextConfigLocation 提供了读取Spring配置文件的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>

2. 将Action配置在Spring容器中

Spring中对Action的配置如下:

<bean id="studentAction" class="com.example.web.action.StudentAction">
        <property name="studentDao" ref="studentDao"></property>
    </bean>

struts.xml中Action的配置如下:

<!-- Action对应的class要写成配置在Spring中的 -->
        <action name="add" class="studentAction" method="add">
            <result>/hello.jsp</result>
        </action>

至此,SSH框架的整合过程全部完成。

转载于:https://www.cnblogs.com/frank-pei/p/5118135.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值