MyEclipse、Struts2.1、Spring3.0、Hibernate3.3框架整合详细图解:

在线:[url]http://blog.sina.com.cn/s/blog_6ffd47bf0100rkw6.html[/url]

选择jar的步骤:
1.Struts 2:选择[color=red]Struts Core(核心库)和Struts 2 Spring(包含集成Spring插件)[/color]的两个最基本库,其他根据需要可以添加。
2.选择Spring3.0版本,类库选择 [color=red]Spring 3.0 AOP、Spring 3.0 Core、Spring 3.0 Persistence Core、Spring 3.0 Persistence JDBC、Spring 3.0 Web 5个库[/color],并勾选复制到本地WEB-INF/lib目录。
3.选择Hibernate3.3版本,如果要[color=red]使用注解可选中启用annotation[/color],但是会对后续自动生成实体类造成麻烦,不选就是用xml文件形式进行实体类映射,[color=red]默认勾选Hibernate 3.3 Annotation & Entity Manager、Hibernate 3.3 Core Libraries两个类库[/color],复制jar文件到本地WEB-INF/lib文件夹下。

项目结构:
[size=large][b]view->action->service->Dao->Database[/b][/size]

1. 包列表:
antlr-2.7.6.jar
aopalliance-1.0.jar
cglib-2.2.jar
classworlds-1.1.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.httpclient-3.1.0.jar
com.springsource.org.apache.commons.lang-2.4.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
commons-beanutils-1.7.0.jar
commons-chain-1.2.jar
commons-collections-3.2.jar
commons-dbcp.jar
commons-digester-2.0.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
freemarker-2.3.16.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-validator.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jotm.jar
json-lib-2.1-jdk15.jar
jta-1.1.jar
log4j-1.2.14.jar
mysql-connector-java-5.0.8-bin.jar
ognl-3.0.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.aspects-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.instrument-3.0.5.RELEASE.jar
org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.oxm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.portlet-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web.struts-3.0.5.RELEASE.jar
oro-2.0.8.jar
persistence.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-web-2.5.6.jar
sslext-1.2-0.jar
struts2-convention-plugin-2.2.1.jar
struts2-core-2.2.1.jar
struts2-embeddedjsp-plugin-2.2.1.jar
struts2-json-plugin-2.2.1.jar
struts2-spring-plugin-2.2.1.jar
xapool.jar
xwork-core-2.2.1.jar
自动生成的时候,记得去掉低版本的重复包,余下的75个jar.
2. web.xml
-------------------------------------

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

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



3. struts.xml 配置的Action的时候,class属性只给出beanId的名字就可以了。



4. applicationContext.xml,配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	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-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.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/asl_daily_report">
		</property>
		<property name="username" value="root"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/xml/NewsInf.hbm.xml</value>
			</list>
		</property>
	</bean>
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
	    <property name="sessionFactory" ref="sessionFactory"/>  
	</bean> 
	<!-- #########################  DAO类配置  ############################### -->
	<bean id="newsInfDAO" class="com.dao.impl.NewsInfDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<!-- #########################  Service类配置  ############################### -->
	<bean id="newsInfoService" class="com.service.impl.NewsInfoService">
		<property name="newsInfDAO" ref="newsInfDAO"></property>
	</bean>

	<!-- #########################  Action类配置  ############################### -->
	<bean id="newsInfAction" class="com.action.NewsInfAction">
		<property name="newsInfoService" ref="newsInfoService"></property>
	</bean>


	<!-- #########################  Spring事务配置  ############################### -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut  id="allServicesMethods" expression="execution(* com.service.impl.*.*(..))"/>
		<aop:advisor advice-ref="txAdvice"  pointcut-ref="allServicesMethods"/>
	</aop:config>	
</beans>



5. 获得HibernateTemplate的方法


I:

private HibernateTemplate hibernateTemplate;


II:

@Resource(name="hibernateTemplate")  
		public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
			this.hibernateTemplate = hibernateTemplate;  
		}



6. 在Struts2获得WebApplicationContext的方法


I: Action实现implements ServletRequestAware,并实现setServletRequest()方法。


II:

public void setServletRequest(HttpServletRequest request) {  
			// TODO Auto-generated method stub  
			this.request = request;  

		}


III:以下是获得的方法


WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());  
		ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());//失败时抛出异常  
		ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); //失败时返回null 
		WebApplicationContext webApplicationContext = (WebApplicationContext)request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);




[b]Spring3跟hibernate整合的配置总结[/b]:其实配置一个Dao需要4个Bean。数据源Bean被装配到会话工厂Bean;会话工厂Bean被装配到HibernateTemplate;最后HibernateTemplate被装配到Dao的Bean。为了让事情变得简单一些,Spring停工了HibernateDaoSupport,它能够让我们把会话工厂Bean直接装配到Dao类,它会自动创建一个HibernateTemplate供Dao使用。只需要Dao集成HibernateDaoSupport类就可以了,在使用HibernateTemplate时调用getHibernateTemplate() 方法即可。