追随Java
简单的事情重复做,就会产生累积效应!
posts - 260,  comments - 338,  trackbacks - 0

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

    
<display-name>shipment</display-name>

    
<!-- Define the basename for a resource bundle for I18N -->
    
<context-param>
        
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        
<param-value>messages</param-value>
    
</context-param>
<!--加载spring+hibernate的配置文件-->
  
    <context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>
            /WEB-INF/applicationContext*.xml
            /WEB-INF/action-servlet.xml
        
</param-value>
    
</context-param>
<!-- 把webwork与spring结合起来-->
    
<listener>
        
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
</listener>
    
<listener>
        
<listener-class>com.opensymphony.xwork.spring.SpringObjectFactoryListener</listener-class>
    
</listener>
<!-- 所有的请求响应的类-->
    
<servlet>
        
<servlet-name>action</servlet-name>
        
<servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
        
<load-on-startup>1</load-on-startup>
    
</servlet>

  
    <servlet-mapping>
        
<servlet-name>action</servlet-name>
        
<url-pattern>*.html</url-pattern>
    
</servlet-mapping>

    
<welcome-file-list>
        
<welcome-file>login.jsp</welcome-file>
    
</welcome-file-list>

</web-app>

  2) xwork.xml用来配置action
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" 
"http://www.opensymphony.com/xwork/xwork-1.0.dtd"
>

<xwork>
      
<!-- Include webwork defaults (from WebWork-2.1 JAR). -->
      
<include file="webwork-default.xml"/>
      
      
<!-- Configuration for the default package. -->
      
<package name="default" extends="webwork-default">
        
<interceptors>
            
<!-- Interceptor to catch exceptions and display mapped exception result -->
            
<interceptor name="exceptionHandler" class="exceptionInterceptor"/>
            
<interceptor-stack name="defaultExceptionStack">
                
<interceptor-ref name="defaultStack"/>
                
<interceptor-ref name="exceptionHandler"/>
            
</interceptor-stack>
        
</interceptors>
        
<!-- Default interceptor stack. --> 
        
<default-interceptor-ref name="defaultExceptionStack"/> 
        
        
<global-results> 
            
<result name="dataAccessFailure">dataAccessFailure.jsp</result>
        
</global-results> 
           
<!--- shipment2 -->
         
 <action name = "login" class= "loginAction" method="login">
                     
<result name="success" type ="dispatcher">shipmentv.jsp</result>
                     
<result name="failuer" type="dispatcher">login.jsp</result>
           
</action>
       
 </package>
</xwork>
       3)action-servlet.xml配置action与调用DAO
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd"
>

<beans>
    <bean id="loginAction" class="com.legend.shipment.web.LoginAction" singleton="false">
            
<property name="shipmentManager" ref="shipmentManager"/>
     
</bean>

</beans>
     4)applicationContext.xml配置事务管理 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd"
>

<beans>
    
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        
<property name="location" value="classpath:jdbc.properties"/>
    
</bean>

    
<!-- Transaction template for Managers, from:
         http://blog.exis.com/colin/archives/2004/07/31/concise-transaction-definitions-spring-11/ 
-->
    
<bean id="txProxyTemplate" abstract="true"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        
<property name="transactionManager"><ref bean="transactionManager"/></property>
        
<property name="transactionAttributes">
            
<props>
                
<prop key="save*">PROPAGATION_REQUIRED</prop>
                
<prop key="remove*">PROPAGATION_REQUIRED</prop>
                
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            
</props>
        
</property>
    
</bean>

    
<bean id="shipmentManager" parent="txProxyTemplate">
        
<property name="target">
            
<bean class="com.legend.shipment.service.impl.ShipmentManagerImpl">
                
<property name="shipmentDAO" ref="shipmentDAO"/>
            
</bean>
        
</property>
    
</bean>
</beans>
         5)applicationContext-hibernate.xml配置hibernate
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd"
>

<beans>
    
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
<property name="driverClassName" value="${jdbc.driverClassName}"/>
        
<property name="url" value="${jdbc.url}"/>
        
<property name="username" value="${jdbc.username}"/>
        
<property name="password" value="${jdbc.password}"/>
    
</bean>

    
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource" ref="dataSource"/>
        
<property name="mappingResources">
            
<list>
                
<value>com/legend/shipment/TworkCost.hbm.xml</value>
                
<value>com/legend/shipment/TworkConfig.hbm.xml</value>
            
</list>
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                
<prop key="hibernate.show_sql">true</prop>
            
</props>
        
</property>
    
</bean>

    
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory"/>
    
</bean>

    
<bean id="shipmentDAO" class="com.legend.shipment.dao.hibernate.ShipmentDAOHibernate">
        
<property name="sessionFactory" ref="sessionFactory"/>
    
</bean>
</beans>
       6)jdbc.properties数据库连接信息
jdbc.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
jdbc.url
=jdbc:microsoft:sqlserver://192.168.1.6:1433;DatabaseName=dg_labour;SelectMethod=cursor;
jdbc.username=sa
jdbc.password
=test
  如果要连接两个数据库的话,那么就加多一个类似applicationContext-hibernate.xml文件,一个类似applicationContext.xml的文件,并在jdbc.properties文件中添加另一个数据库的连接信息。当然要作小的修改。比如说:你在applicationContext-another.xml加了一个名为workManager的事务代理。你想把它添加到loginAction中去,那么就把action_server.xml文件修改成
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd"
>

<beans>
    
<bean id="loginAction" class="com.legend.shipment.web.LoginAction" singleton="false">
            
<property name="shipmentManager" ref="shipmentManager"/>
    <property name="workManager" ref="workManager"/>   
     
</bean>

</beans>
在java程序中就可以通过setWorkManager()方法自己设置了,也就可以在LoginAction类中引用了。
可能说的不够详细,但提供了一个思路。
posted on 2005-09-08 16:10 Harryson 阅读(5178) 评论(0)  编辑  收藏 所属分类: HibernateSpring

只有注册用户登录后才能发表评论。


网站导航:
 

<2005年9月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(10)

随笔分类(319)

AJAX

Coffee House

CSS

Java

JavaScript

Open Source

ProjectManagement

友情Blog

最新随笔

最新评论

阅读排行榜

评论排行榜