Struts2.3.4+Hibernate3.6+Spring2.5.6 集成教程

bernate与spring进行集成
1、导入hibernate与spring的jar文件
hibernate的jar文件:
log4j-1.2.15.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate3.jar
antlr-2.7.6.jar
spring的jar文件:
spring.jar
aspectjrt.jar
aspectjweaver.jar
commons-dbcp.jar
commons-logging.jar
commons-pool.jar
commons-lang3-3.1.jar


2、在src下新建applicationContext.xml文件
        <!-- 配置数据源 -->
<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/users?useUnicode=true&characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="java"></property>
</bean>
<!-- 配置sessionFactory,并将数据源注入 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>com/minty/entity/Userinfo.hbm.xml</value>
</list>
</property>
<!-- 设置hiebernate常用属性 -->
<property name="hibernateProperties">
<props>
<!-- 设置hibernate方言类 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>






 
①并在dao的实现类中声明HibernateTemplate常量,并生成setter方法
private HibernateTemplate hibernateTemplate;


public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}




②在biz的实现类中调用dao,并生成setter方法
private UserinfoDao userinfoDao;


public void setUserinfoDao(UserinfoDao userinfoDao) {
this.userinfoDao = userinfoDao;
}




4.在applicationContext.xml配置文件中 配置biz 与 dao 并将hibernateTemplate注入


<!-- 配置HibernateTemplate 并将sessionFactory注入-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置dao,并将hibernateTemplate注入 -->
<bean id="userinfoDao" class="com.minty.dao.impl.UserinfoDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 配置biz,并将dao注入 -->
<bean id="userinfoBiz" class="com.minty.biz.impl.UserinfoBizImpl">
<property name="userinfoDao" ref="userinfoDao"></property>
</bean>




5.到此hibernate与spring就集成成功,继续集成struts2
还是需要我们的jar文件:
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.12.0.GA.jar
struts2-core-2.3.4.1.jar
struts2-spring-plugin-2.3.4.1.jar(此jar文件为关键)
下面就配置struts的核心控制器在web.xml中配置
<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>




6.在applicationContext.xml中配置
<!-- 配置声明式事务 -->
<!-- 配置事务管理器,并将sessionFactory注入 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置方面通知 ,并将transactionManager注入-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 此处的配置是拦截action中的方法,必须以name中的字母开始才进行拦截 -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="select*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 ,并将方面通知与切面告知通知者-->
<aop:config>
<!-- 配置切面 -->
<aop:pointcut id="pointCut" expression="execution(* com.minty.biz.*.*(..))" />
<!-- 将切面与方面通知告知通知者 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
</aop:config>




7.写action类与配置struts.xml文件
action的类在此不再详细写出
写完action类,需要在applicationContex.xml中注入biz
<!-- 配置action,并将biz注入 -->
<bean id="userinfoAction" class="com.minty.action.UserinfoAction">
<property name="userinfoBiz" ref="userinfoBiz"></property>
</bean>




配置struts.xml文件
<struts>
  <package name="sshDemo" extends="struts-default">
  <!-- 此处的class="action的全类名或者是applicationContext.xml中配置action的id的value" 建议使用配置文件中action的id value -->
  <action name="userinfo_*" class="userinfoAction" method="{1}">
  <result name="success">/list.jsp</result>
  <result name="error">/error.jsp</result>
  </action>
  </package>
</struts>






8.在web.xml中配置监听器并解决懒加载问题
<!-- 配置ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置加载spring配置文件的加载路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<!-- 解决懒加载问题OpenSessionInViewFilter -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



后附源码:

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: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/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="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/users?useUnicode=true&characterEncoding=utf-8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="java"></property>
	</bean>
	<!-- 配置sessionFactory,并将数据源注入 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 引入映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/minty/entity/Userinfo.hbm.xml</value>
			</list>
		</property>
		<!-- 设置hiebernate常用属性 -->
		<property name="hibernateProperties">
			<props>
				<!-- 设置hibernate方言类 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置HibernateTemplate 并将sessionFactory注入-->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置dao,并将hibernateTemplate注入 -->
	<bean id="userinfoDao" class="com.minty.dao.impl.UserinfoDaoImpl">
		<property name="hibernateTemplate" ref="hibernateTemplate"></property>
	</bean>
	<!-- 配置biz,并将dao注入 -->
	<bean id="userinfoBiz" class="com.minty.biz.impl.UserinfoBizImpl">
		<property name="userinfoDao" ref="userinfoDao"></property>
	</bean>
	
	<!-- 配置action,并将biz注入 -->
	<bean id="userinfoAction" class="com.minty.action.UserinfoAction">
		<property name="userinfoBiz" ref="userinfoBiz"></property>
	</bean>


	<!-- 配置声明式事务 -->
	<!-- 配置事务管理器,并将sessionFactory注入 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 配置方面通知 ,并将transactionManager注入-->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 此处的配置是拦截action中的方法,必须以name中的字母开始才进行拦截 -->
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="select*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="do*" propagation="REQUIRED" />
			<tx:method name="*" propagation="SUPPORTS"/>
		</tx:attributes>
	</tx:advice>
	<!-- 配置切面 ,并将方面通知与切面告知通知者-->
	<aop:config>
		<!-- 配置切面 -->
		<aop:pointcut id="pointCut" expression="execution(* com.minty.biz.*.*(..))" />
		<!-- 将切面与方面通知告知通知者 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
	</aop:config>
</beans>



web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<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>
	
	<!-- 配置ContextLoaderListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置加载spring配置文件的加载路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext*.xml</param-value>
	</context-param>
	
	<!-- 解决懒加载问题OpenSessionInViewFilter -->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>



项目结构截图:



所需jar文件截图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值