Struts2,Spring, Hibernate三大框架SSH的整合步骤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2,Spring, Hibernate三大框架SSH的整合步骤相关的知识,希望对你有一定的参考价值。
整合步骤
- 创建web工程
- 引入相应的jar包
- 整合spring和hibernate框架
- 编写实体类pojo和hbm.xml文件
- 编写bean-base.xml文件
<!-- 1) 连接池实例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://mySqlCheck"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>
<!-- 2) SessionFactory实例创建 -->
<!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- a. 连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- c. 映射配置 -->
<property name="mappingLocations">
<list>
<value>classpath:cn/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 3) 事务配置 -->
<!-- # 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- # 事务增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- # AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* cn.service.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
3.编写bean-dao文件
<bean id="employeeDao" class="cn.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
4.编写bean-service文件
<bean id="employeeService" class="cn.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
- 整合spring和struts2框架
1.编写bean-action文件
<bean id="employeeAction" class="cn.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean>
1.编写struts.xml文件
<package name="emp" extends="struts-default">
<!-- action实例交给spring容器创建 -->
<action name="show" class="employeeAction" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
- 配置web.xml文件
<!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
<!-- 注意:访问struts时候需要带上*.action后缀 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
以上是关于Struts2,Spring, Hibernate三大框架SSH的整合步骤的主要内容,如果未能解决你的问题,请参考以下文章
Struts2+Spring+Hibernate(SSH)框架的搭建
struts2集成Spring,Hibernate的问题!!
Spring:Spring整合Hibernate,之后整合Struts2