spring+hibernate下连接两个以上的数据库

新系统可能需使用遗留系统数据,会面临连接两个以上数据库的问题。处理原理是创建类似配置文件,以webwork+spring+hibernate为例,可通过配置web.xml等方式解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新的系统有可能会要用到遗留系统的数据,所以有时就会要连接两个以上的数据库。
那么怎么来处理这一问题呢?
原理:创建类似的文件(两个数据库就有两个类似用spring+hibernate的配置文件)
下面以webwork+spring+hibernate来说明。
1)web.xml

None.gif<?xml version="1.0" encoding="UTF-8"?>
None.gif
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
None.gif    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
None.gif    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
None.gif
None.gif    
<display-name>shipment</display-name>
None.gif
None.gif    
<!-- Define the basename for a resource bundle for I18N -->
None.gif    
<context-param>
None.gif        
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
None.gif        
<param-value>messages</param-value>
None.gif    
</context-param>
None.gif<!--加载spring+hibernate的配置文件-->
None.gif  
    <context-param>
None.gif        
<param-name>contextConfigLocation</param-name>
None.gif        
<param-value>
None.gif            /WEB-INF/applicationContext*.xml
None.gif            /WEB-INF/action-servlet.xml
None.gif        
</param-value>
None.gif    
</context-param>
<!-- 把webwork与spring结合起来-->
None.gif    
<listener>
None.gif        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
None.gif    
</listener>
None.gif    
<listener>
None.gif        
<listener-class>com.opensymphony.xwork.spring.SpringObjectFactoryListener</listener-class>
None.gif    
</listener>
<!-- 所有的请求响应的类-->None.gif
None.gif    
<servlet>
None.gif        
<servlet-name>action</servlet-name>
None.gif        
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
None.gif        
<load-on-startup>1</load-on-startup>
None.gif    
</servlet>
None.gif
None.gif  
    <servlet-mapping>
None.gif        
<servlet-name>action</servlet-name>
None.gif        
<url-pattern>*.html</url-pattern>
None.gif    
</servlet-mapping>
None.gif
None.gif    
<welcome-file-list>
None.gif        
<welcome-file>login.jsp</welcome-file>
None.gif    
</welcome-file-list>
None.gif
None.gif
</web-app>
None.gif

  2) xwork.xml用来配置action
None.gif<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" 
None.gif"http://www.opensymphony.com/xwork/xwork-1.0.dtd"
>
None.gif
None.gif
<xwork>
None.gif      
<!-- Include webwork defaults (from WebWork-2.1 JAR). -->
None.gif      
<include file="webwork-default.xml"/>
None.gif      
None.gif      
<!-- Configuration for the default package. -->
None.gif      
<package name="default" extends="webwork-default">
None.gif        
<interceptors>
None.gif            
<!-- Interceptor to catch exceptions and display mapped exception result -->
None.gif            
<interceptor name="exceptionHandler" class="exceptionInterceptor"/>
None.gif            
<interceptor-stack name="defaultExceptionStack">
None.gif                
<interceptor-ref name="defaultStack"/>
None.gif                
<interceptor-ref name="exceptionHandler"/>
None.gif            
</interceptor-stack>
None.gif        
</interceptors>
None.gif        
<!-- Default interceptor stack. --> 
None.gif        
<default-interceptor-ref name="defaultExceptionStack"/> 
None.gif        
None.gif        
<global-results> 
None.gif            
<result name="dataAccessFailure">dataAccessFailure.jsp</result>
None.gif        
</global-results> 
None.gif           
<!--- shipment2 -->
None.gif         
 <action name = "login" class= "loginAction" method="login">
None.gif                     
<result name="success" type ="dispatcher">shipmentv.jsp</result>
None.gif                     
<result name="failuer" type="dispatcher">login.jsp</result>
None.gif           
</action>
None.gif       
 </package>
None.gif
</xwork>
None.gif
       3)action-servlet.xml配置action与调用DAO
None.gif<?xml version="1.0" encoding="UTF-8"?>
None.gif
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
None.gif    "http://www.springframework.org/dtd/spring-beans.dtd"
>
None.gif
None.gif
<beans>
None.gif
    <bean id="loginAction" class="com.legend.shipment.web.LoginAction" singleton="false">
None.gif            
<property name="shipmentManager" ref="shipmentManager"/>
None.gif     
</bean>
None.gif
None.gif
</beans>
None.gif
     4)applicationContext.xml配置事务管理 
None.gif<?xml version="1.0" encoding="UTF-8"?>
None.gif
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
None.gif    "http://www.springframework.org/dtd/spring-beans.dtd"
>
None.gif
None.gif
<beans>
None.gif    
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
None.gif        
<property name="location" value="classpath:jdbc.properties"/>
None.gif    
</bean>
None.gif
None.gif    
<!-- Transaction template for Managers, from:
None.gif         http://blog.exis.com/colin/archives/2004/07/31/concise-transaction-definitions-spring-11/ 
-->
None.gif    
<bean id="txProxyTemplate" abstract="true"
None.gif        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
None.gif        
<property name="transactionManager"><ref bean="transactionManager"/></property>
None.gif        
<property name="transactionAttributes">
None.gif            
<props>
None.gif                
<prop key="save*">PROPAGATION_REQUIRED</prop>
None.gif                
<prop key="remove*">PROPAGATION_REQUIRED</prop>
None.gif                
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
None.gif            
</props>
None.gif        
</property>
None.gif    
</bean>
None.gif
None.gif    
<bean id="shipmentManager" parent="txProxyTemplate">
None.gif        
<property name="target">
None.gif            
<bean class="com.legend.shipment.service.impl.ShipmentManagerImpl">
None.gif                
<property name="shipmentDAO" ref="shipmentDAO"/>
None.gif            
</bean>
None.gif        
</property>
None.gif    
</bean>
None.gif
</beans>
None.gif
         5)applicationContext-hibernate.xml配置hibernate
None.gif<?xml version="1.0" encoding="UTF-8"?>
None.gif
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
None.gif    "http://www.springframework.org/dtd/spring-beans.dtd"
>
None.gif
None.gif
<beans>
None.gif    
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
None.gif        
<property name="driverClassName" value="${jdbc.driverClassName}"/>
None.gif        
<property name="url" value="${jdbc.url}"/>
None.gif        
<property name="username" value="${jdbc.username}"/>
None.gif        
<property name="password" value="${jdbc.password}"/>
None.gif    
</bean>
None.gif
None.gif    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
None.gif        
<property name="dataSource" ref="dataSource"/>
None.gif        
<property name="mappingResources">
None.gif            
<list>
None.gif                
<value>com/legend/shipment/TworkCost.hbm.xml</value>
None.gif                
<value>com/legend/shipment/TworkConfig.hbm.xml</value>
None.gif            
</list>
None.gif        
</property>
None.gif        
<property name="hibernateProperties">
None.gif            
<props>
None.gif                
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
None.gif                
<prop key="hibernate.show_sql">true</prop>
None.gif            
</props>
None.gif        
</property>
None.gif    
</bean>
None.gif
None.gif    
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
None.gif        
<property name="sessionFactory" ref="sessionFactory"/>
None.gif    
</bean>
None.gif
None.gif    
<bean id="shipmentDAO" class="com.legend.shipment.dao.hibernate.ShipmentDAOHibernate">
None.gif        
<property name="sessionFactory" ref="sessionFactory"/>
None.gif    
</bean>
None.gif
</beans>
None.gif
       6)jdbc.properties数据库连接信息
None.gifjdbc.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
None.gifjdbc.url
=jdbc:microsoft:sqlserver://192.168.1.6:1433;DatabaseName=dg_labour;SelectMethod=cursor;
None.gif
jdbc.username=sa
None.gifjdbc.password
=test
  如果要连接两个数据库的话,那么就加多一个类似applicationContext-hibernate.xml文件,一个类似applicationContext.xml的文件,并在jdbc.properties文件中添加另一个数据库的连接信息。当然要作小的修改。比如说:你在applicationContext-another.xml加了一个名为workManager的事务代理。你想把它添加到loginAction中去,那么就把action_server.xml文件修改成
None.gif<?xml version="1.0" encoding="UTF-8"?>
None.gif
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
None.gif    "http://www.springframework.org/dtd/spring-beans.dtd"
>
None.gif
None.gif
<beans>
None.gif    
<bean id="loginAction" class="com.legend.shipment.web.LoginAction" singleton="false">
None.gif            
<property name="shipmentManager" ref="shipmentManager"/>
    <property name="workManager" ref="workManager"/>   
None.gif     
</bean>
None.gif
None.gif
</beans>
None.gif
在java程序中就可以通过setWorkManager()方法自己设置了,也就可以在LoginAction类中引用了。
可能说的不够详细,但提供了一个思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值