struts 2.3+spring 3.2.4+hibernate 4.2.8 环境配置

在上一篇文章中已经集成 struts 2.3+spring 3.2.4 ,这篇中继续集成hibernate 4.2.8。
1 集成 hibernate 4.2.8所需要的jar包 
struts 2.3+spring 3.2.4+hibernate 4.2.8 环境配置 - chy2z - 黑暗行动
2 项目结构
  struts 2.3+spring 3.2.4+hibernate 4.2.8 环境配置 - chy2z - 黑暗行动
 

3  web.xml 添加如下配置

 <!-- hibernate -->
   <filter>
<filter-name>openSessionInView</filter-name>
<filter-class> org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
  </init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>   
            <param-value>sessionFactoryMy</param-value>  
  </init-param>
<init-param> 
        <param-name>flushMode</param-name> 
    <param-value>COMMIT</param-value>         
        </init-param> 
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

4  hibernate.my.cfg.xml  配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.jdbc.batch_size">1</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">false</property>    
    </session-factory>
</hibernate-configuration>

5 jdbc.my.properties 配置如下(访问sqlserver)
my.jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
my.jdbc.url=jdbc:sqlserver://localhost:1433;databaseName=sshFrame
my.jdbc.username=sa
my.jdbc.password=123

6 hibernate-common.cfg.xml 配置如下 ( Map映射文件 )
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- <mapping resource="hibernate/common/mapping/chy.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>

7 spring-hibernate.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"
xmlns:datasource="http://www.iframeworks.org/schema/datasource" xmlns:hibernate="http://www.iframeworks.org/schema/orm/hibernate"
xmlns:transaction="http://www.iframeworks.org/schema/transaction" xmlns:cache="http://www.iframeworks.org/schema/cache"
xmlns:log="http://www.iframeworks.org/schema/log"
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="propertyConfigurerMy" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="order" value="1" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value> classpath:hibernate/jdbc.my.properties</value>
</list>
</property>
</bean>
<!-- schedule datasource configuration c3p0 -->
<bean id="dataSourceMy" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"    value="${my.jdbc.driverClassName}" ></property>
<property name="url" value="${my.jdbc.url}" ></property>
<property name="username"    value="${my.jdbc.username}" ></property>
<property name="password"    value="${my.jdbc.password}" ></property>
</bean> 
<bean id="sessionFactoryMy" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource"> <ref bean="dataSourceMy" /> </property>
<property name="configLocations">
<list>
    <value>classpath:/hibernate /hibernate.my.cfg.xml</value>
   <value>classpath:/hibernate/common/ hibernate-common.cfg.xml</value> <!-- Map映射文件 -->
</list>
</property>
</bean>

<bean id="transactionManagerMy" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactoryMy" />
    <property name="dataSource" ref="dataSourceMy" />
</bean>

<tx:advice id="txAdviceMy" transaction-manager="transactionManagerMy">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="publish*" propagation="REQUIRED" />
<tx:method name="write*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
</beans>
 
8  spring-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"
xmlns:datasource="http://www.iframeworks.org/schema/datasource" xmlns:hibernate="http://www.iframeworks.org/schema/orm/hibernate"
xmlns:transaction="http://www.iframeworks.org/schema/transaction" xmlns:cache="http://www.iframeworks.org/schema/cache"
xmlns:log="http://www.iframeworks.org/schema/log"
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">
<!-- <import resource="spring/spring-main-applicationcontext.xml"/> --> <!-- 定义bean -->
<import resource="spring/spring-hibernate.xml"/> <!-- hibernate  配置  -->
</beans>

完成以上配置,就可以正确运行了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值