struts2+hibernate4+spring4整合步骤

1 篇文章 0 订阅

spring整合hibernate

 

一、加入sping

1、加入springjar,再加入spring集成hibernatejar

spring集成hibernatejar

 

2、配置web.xml,注意:application*.xml,可以在启动容器时实现多个配置文件的引入。

</pre><span style="white-space:pre">			</span><pre name="code" class="html">			<!-- needed for ContextLoaderListener -->
			<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:applicationContext*.xml</param-value>
			</context-param>
		
			<!-- Bootstraps the root web application context before servlet initialization -->
			<listener>
				<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
			</listener>


3src下加入applicationContext.xml

 

 

二、加入hibernate

1、加入jar

2、配置hibernate.cfg.xml文件

<session-factory>
   <!-- 配置hibernate的基本属性-->
           <!-- 配置方言 -->
           <propertyname="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
           <!-- 配置 显示sql以及格式化-->
           <propertyname="hibernate.show_sql">true</property>
           <propertyname="hibernate.format_sql">true</property>
           <!-- 数据表生成策略 -->
           <propertyname="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
 


3、建立持久化类,以及相应的*.hbm.xml文件

注意:持久化类的关联关系

3、和spring整合

加入c3p0mysql-connection.jar



       ② 配置数据源, sessionFactory ,声明式事务

 

<!--导入源文件db.properties  -->
<context:property-placeholderlocation="classpath:db.properties"/>
 
<!-- 配置数据源-->
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="user" value="${jdbc.user}"></property>
<propertyname="password"value="${jdbc.password}"></property>
<propertyname="driverClass"value="${jdbc.driverClass}"></property>
<propertyname="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 
 
<propertyname="initialPoolSize"value="${jdbc.initPoolSize}"></property>
<propertyname="maxPoolSize"value="${jdbc.maxPoolSize}"></property>
</bean>

<!--配置sessionFactory

 三个元素

dataSourcehibernate.cfg.xml*hbm.xml

-->

<bean id="sessionFactory" class=
"org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 
<propertyname="dataSource" ref="dataSource"></property>
<propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property>
<propertyname="mappingLocations"value="classpath:cn/edu/scnu/pojo/*.hbm.xml"></property>
 
</bean>
 
<!-- 配置声明式事务 -->
<bean id="transactionManager" class=
	"org.springframework.orm.hibernate4.HibernateTransactionManager">
				
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
			
<!-- 配置事务切面(aop) -->
<aop:config>
	<aop:advisor advice-ref="" pointcut-ref=""/>
</aop:config>


声明式事务的最终配置:


配置到sessionFactory时启动项目,就会生成相对应的数据表。

 

四、spring整合struts2

1、加入jar包,如果有重复的需要删掉多余的jar

2、配置web.xml,配置struts2Filter

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

4、整合spring

加入struts2spring插件的jar

Spring的配置文件中正常配置Action,注意:Actionscopepropotype

struts2的配置文件中配置action时,class属性指向该actionIOC容器中的id

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值