`

配置Springframework与hibernate连接多数据库的事务

阅读更多
本文只注重配置,分别有如下几个文件
<出处:和巍雅的BLOG
web.xml 部署配置文件
struts-config.xml  struts的配置文件
action-servlet.xml Spring依赖注入struts
Nhibernate_Context.xml  Spring依赖注入struts
hibernate.cfg.xml    mssql的Hibernate配置
hibernate_mysql.cfg.xml mysql的Hibernate配置 
开始:
web.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>EAIJ2EEPRJWeb</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Nhibernate_Context.xml</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<display-name>SetCharacterEncodingFilter</display-name>
<filter-class>com.throuhout.j2eemodel.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<display-name>SpringContextServlet</display-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>
---------------------------------------------------------------------
struts_config.xml
---------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
                               "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- 数据源 -->
<data-sources></data-sources>

<!-- 表单 Bean -->
<form-beans>
<form-bean name="UserActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="user" type="com.throuhout.j2eemodel.hibernate.TSysuser" />
</form-bean>
<form-bean name="UserMenuActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="menu" type="com.throuhout.j2eemodel.hibernate.TSysmufudef">
</form-property>
</form-bean>
</form-beans>

<!-- 全局异常 -->
<global-exceptions></global-exceptions>


<!-- 全局转发 -->
<global-forwards>
<forward name="success" path="/main/success.jsp"></forward>
<forward name="failure" path="/main/fail.jsp"></forward>
</global-forwards>

<!-- 操作映射 -->
<action-mappings>
<action path="/User" type="org.springframework.web.struts.DelegatingActionProxy" name="UserActionForm" scope="request" parameter="method" validate="false">
<forward name="sucess" path="/main/project_index.htm" />
<forward name="false" path="/failure.html"></forward>
<forward name="list" path="/main/userlist.jsp">
</forward>
<forward name="edit" path="/main/userform.jsp">
</forward>
</action>
<action path="/UserMenu" type="org.springframework.web.struts.DelegatingActionProxy" name="UserMenuActionForm" scope="request" parameter="method" validate="false">
<forward name="menu" path="/main/menu.jsp"></forward>
<forward name="usermenu" path="/main/usermenu.jsp">
</forward>
</action>
</action-mappings>

<!-- 消息资源 -->
<message-resources parameter="eaij2eeprjweb.resources.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml" />
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
</plug-in>

</struts-config>
-------------------------------------------------------------------
action-servlet.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">

<!-- Spring依赖注入到Struts当中 -->
<beans>
<bean name="/User" class="com.throuhout.j2eemodel.struts.action.UserAction" singleton="false">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
<bean name="/UserMenu" class="com.throuhout.j2eemodel.struts.action.UserMenuAction" singleton="false">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>

</beans>
-------------------------------------------------------------------
Nhibernate_Context.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Spring依赖注入到Struts当中 -->
<!-- Hibernate SessionFactory -->
<beans>
<bean id="mssqlSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</prop>

<!--<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>-->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
</props>
</property>
</bean>

<bean id="mysqlSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate_mysql.cfg.xml</value>
</property>

<property name="hibernateProperties">
<props>
<!--<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>-->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->

<bean id="mysqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>

<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>

<!--
<bean id="aDao" class="com.plat.dao.user.impl.aDaoImpl">
<property name="sessionFactory">
<ref local="oracleSessionFactory" />
</property>
</bean>
-->
<bean id="userDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.UserHibernateDao">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>


<!-- Service -->
<bean id="userManagerTarget" class="com.throuhout.j2eemodel.spring.service.UserManagerImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>

<bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mysqltransactionManager" />
</property>

<property name="target">
<ref local="userManagerTarget" />
</property>

<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
---------------------------------------------------------------------
hibernate.cfg.xml
---------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>

<property name="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</property>

<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.100.111:1433/testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">yaya</property>
<!--
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.100.148:1433/throughoutdb</property>
<property name="hibernate.connection.username">siaote</property>
<property name="hibernate.connection.password">siaote</property>
-->
<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">mssqlSessionFactory</property>

<!-- Mapping files -->
<mapping resource="hbm_tb/T_COST_TYPE.hbm.xml" />


</session-factory>
</hibernate-configuration>
-------------------------------------------------------------------
hibernate_mysql.cfg.xml
-------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>

<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost/pdm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>

<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">mysqlSessionFactory</property>

<!-- Mapping files -->
<mapping resource="hbm_tb/TSysuser.hbm.xml" />
<mapping resource="hbm_tb/TSysmufudef.hbm.xml" />


</session-factory>
</hibernate-configuration>

----------------------------------------------------------------
本人的开发环境:
开发工具:WSAD 5.1
中间件服务器:tomcat 4.24
数据库:mysql\mssql 完全通过。
你可以和 heweiya@yahoo.com.cn 联系
分享到:
评论

相关推荐

    spring3,hibernate4 配置声明式事务管理(annotation方式)

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect <prop key="hibernate.show_sql...

    在Spring中配置Hibernate事务

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> ``` 4. **启用Spring的AOP代理**: 声明式事务管理基于Spring的AOP,因此需要开启AOP代理。这...

    org.springframework.orm.hibernate3.LocalSessionFactoryBean

    在开发基于Spring与Hibernate整合的应用时,可能会遇到“`org.springframework.orm.hibernate3.LocalSessionFactoryBean` not found”这样的错误提示。这个问题通常发生在尝试通过Spring管理Hibernate ...

    现有Mysql数据库,写Spring + Hibernate的配置文件

    HibernateTransactionManager则负责管理数据库事务。 ```xml <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> ;serverTimezone=UTC"/> ...

    Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)

    而Hibernate则是一款强大的对象关系映射(ORM)工具,它简化了数据库操作,使开发者可以使用面向对象的方式来处理数据库事务。本篇文章将重点讨论如何在Spring框架中集成Hibernate,并探讨编程式事务管理和Spring ...

    spring3+hibernate4声明式事务配置(xml方式)

    与编程式事务管理不同,声明式事务管理允许开发者通过注解或XML配置来声明事务边界,而无需在业务逻辑代码中显式处理事务的开始、提交、回滚等操作。这样可以提高代码的可读性和可维护性。 接下来,我们将逐步解析...

    org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

    标题中的"org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException"是一个具体的异常类型,源自Spring框架的Hibernate4模块。这个异常通常在乐观锁(Optimistic Locking)机制失败时抛出,...

    第25,26讲 --搭建和配置Spring与Hibernate整合的环境

    在`hibernate.cfg.xml`中,我们需要定义Hibernate的配置,包括数据库连接信息、实体类扫描路径等: ```xml <hibernate-configuration> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect...

    spring3与hibernate3整合全部jar包

    这些jar包的整合过程包括配置Spring的XML文件以管理Hibernate SessionFactory,设置数据源,配置事务管理器,并在Hibernate的配置文件中指定实体类和数据库连接信息。在实际应用中,开发者还需要编写DAO(数据访问...

    HibernateSpring多数据库解决方案.doc

    下面我们将深入探讨如何在Spring和Hibernate环境下配置和管理多个数据库。 首先,Spring框架提供了数据源(DataSource)的概念,允许我们配置多个数据源来连接不同的数据库。在给出的文档中,我们可以看到有两个...

    Spring与Hibernate集成---声明式事务

    SessionFactory是从Hibernate的配置文件中读取数据库连接信息并创建的,而HibernateTransactionManager则负责处理与Hibernate相关的事务。 ```xml <bean id="sessionFactory" class="org.springframework.orm....

    Spring Hibernate事务实例

    本教程将深入探讨如何在Spring框架中利用`TransactionInterceptor`进行声明式事务管理,与Hibernate集成实现高效的数据库事务控制。 首先,了解事务管理是至关重要的。事务是一组数据库操作,这些操作要么全部成功...

    springmvc+hibernate(连接sql)的增删改

    3. 数据库连接配置,如JDBC驱动和数据源。 4. Controller、Service和DAO层的设计和实现。 5. Spring的依赖注入和AOP(面向切面编程)特性。 6. SQL查询和事务处理。 对于想要深入学习Spring MVC和Hibernate的开发者...

    利用JSF、SpringFramework和Hibernate构建Web应用的实例讲述

    - **作用**:Spring Framework 在项目中起到连接各个组件的作用,可以作为JSF和Hibernate之间的桥梁,使得整体架构更加清晰和易于维护。 #### 3. Hibernate 概念及其作用 - **定义**:Hibernate 是一个流行的 Java...

    Spring Framework v6.1.4.zip

    例如,Spring Data JPA使得与JPA供应商(如Hibernate)的集成变得简单,而Spring Data MongoDB则为MongoDB提供了类似的便利。 此外,Spring Security提供了全面的安全管理功能,包括身份验证、授权和安全配置,确保...

    spring-mvc hibernate配置

    - **hibernate.cfg.xml**:配置Hibernate的核心配置,包括数据库连接信息、方言、缓存策略、实体扫描路径等。 - **实体类和映射文件**:创建Java类表示数据库表,并使用Hibernate的注解(如@Entity、@Table、@...

    spring对多个数据库进行事务管理.doc

    Spring 框架确实提供了对多个数据库事务管理的支持,这对于多数据源的应用场景非常关键。在Spring中,事务管理可以通过编程式和声明式两种方式进行。编程式事务管理需要程序员手动控制事务的开始、提交、回滚等操作...

    spring配置hibernate事务

    在Spring框架中配置Hibernate事务管理是一项关键任务,它允许开发者以声明式的方式处理数据库操作的事务性,确保数据的一致性和完整性。以下是如何在Spring中配置Hibernate事务的详细步骤和概念解释。 首先,理解...

    JSF集成Spring_Framework和Hibernate开发应用实例

    Spring Framework是一个全功能的后端框架,提供了依赖注入(DI)、面向切面编程(AOP)、事务管理等核心功能,同时支持多种数据访问技术,包括Hibernate。Hibernate则是一个对象关系映射(ORM)工具,能够将数据库...

Global site tag (gtag.js) - Google Analytics