如何整合Struts2 Hibernate 和Spring 首先你必须了解三个框架是如何独立运行的:
代码下载:http://download.csdn.net/detail/zhanglinlang/9775160
Struts2 是通过在web.xml 中 Filter拦截器来启动自己的核心类StrutsPrepareAndExecuteFilter,完成事件的分发。
Spring 是通过 在web.xml 注册监听器来监听ContextLoaderListener,监听web应用的启动事件来完成自己的框架启动工作。
Hibernate 是通过创建SessionFactory工厂手动加载配置文件来完成启动工作,Hibernate是可以通过代码创建SessionFactory,就可以交给Spring通过ioc来控制该类的实例化。
首先在resource目录下创建 config目录方便管理:
首先配置
Hibernate :
<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hbm2ddl.auto">updateproperty>
<property name="connection.isolation">4property>
<mapping resource="com/zll/entity/User.hbm.xml"/>
<mapping resource="com/zll/entity/Student.hbm.xml"/>
session-factory>
hibernate-configuration>
Struts.xml
<struts>
<package name="person" namespace="/" extends="struts-default">
<action name="userAction" class="userAction" />
package>
struts>
Spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver">property>
<property name="jdbcUrl" value="jdbc:mysql:///hibernate?characterEncoding=utf-8">property>
<property name="user" value="root">property>
<property name="password" value="root">property>
bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="configLocations" value="classpath:config/hibernate.cfg.xml">property>
bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<import resource="classpath:config/testTemple.xml"/>
beans>
testTemple.xml(这个不重要,看看如何配置就行)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="userAction" class="com.zll.action.UserAction" scope="prototype">
<property name="userService" ref="userService">property>
bean>
<bean id="userService" class="com.zll.service.UserService">
<property name="userDao" ref="userDaoImp">property>
bean>
<bean id="userDaoImp" class="com.zll.dao.UserDaoImp">
<property name="hibernateTemplate" ref="hibernateTemplate">property>
bean>
beans>
最重要的:
web.xml
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:config/spring.xmlparam-value>
context-param>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
<init-param>
<param-name>configparam-name>
<param-value>struts-default.xml,struts-plugin.xml,config/struts.xmlparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
最后pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.zllgroupId>
<artifactId>sshTestartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>sshTest Maven Webappname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.1.4.RELEASEspring.version>
<hibernate.version>4.3.8.Finalhibernate.version>
<struts2.version>2.3.20struts2.version>
<jackson.version>2.5.0jackson.version>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-coreartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-ehcacheartifactId>
<version>${hibernate.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-coreartifactId>
<version>${struts2.version}version>
<exclusions>
<exclusion>
<artifactId>javassistartifactId>
<groupId>javassistgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-spring-pluginartifactId>
<version>${struts2.version}version>
dependency>
<dependency>
<groupId>org.apache.strutsgroupId>
<artifactId>struts2-convention-pluginartifactId>
<version>${struts2.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.34version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5-pre8version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>mchange-commons-javaartifactId>
<version>0.2.12version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson.version}version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.4version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>3.0-alpha-1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
<build>
<finalName>spring_struts2_Hibernate_demofinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.4.2version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>2.3version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xmlwebXml>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
<version>2.9.1version>
<configuration>
<javadocDirectory>target/javadocjavadocDirectory>
<reportOutputDirectory>target/javadocreportOutputDirectory>
<charset>UTF-8charset>
<encoding>UTF-8encoding>
<docencoding>UTF-8docencoding>
<show>privateshow>
configuration>
plugin>
<plugin>
<groupId>org.codehaus.cargogroupId>
<artifactId>cargo-maven2-pluginartifactId>
<version>1.0version>
<configuration>
<container>
<containerId>tomcat6xcontainerId>
<home>F:\apache-tomcat-7.0.27home>
container>
<configuration>
<type>existingtype>
<home>F:\apache-tomcat-7.0.27home>
configuration>
configuration>
plugin>
plugins>
build>
project>
整合细节知识点(了解):
Struts2 和 Spring整合:就是将action的创建交由Spring管理。配置文件中class只需要配置Spring中bean的id就可以。
name="userAction" class="SpringBeanId" />
Hibernate和Spring整合:
1、创建连接池的对象交由spring管理
2、Spring提供了SessionFactory的封装类LocalSessionFactory,配置该类,并将数据源配置给它。
3、配置Spring的HibernateTemplate,将SessionFactory配置给他。替代session完成数据操作。
4、spring 事务管理配置,替代Hibernate事务管理