日期:2014-05-16  浏览次数:20682 次

Hibernate+Spring连接多个数据库
Hibernate数据源配置

连接多数据的配置就是在配置文件中配置两个数据源,两个事务,DAO配置文件中调用哪个就连哪个数据库。

配置如下:


数据源1:


<bean id="dataSourceCG" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
   <property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.210.204)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.210.204)(PORT=1521))(FAILOVER=on)(LOAD_BALANCE=off))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=CNGBL1T2)))" />
   <property name="username" value="SHADB" />
   <property name="password" value="chinaerp2002" />
</bean>


数据源2:


<bean id="dataSourceCGLong" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
   <property name="url" value="jdbc:oracle:thin:@10.101.1.41:1521:CNGBL1T1" />
   <property name="username" value="SHADB" />
   <property name="password" value="chinaerp2002" />
</bean>


数据源1包括的表:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSourceCG" />
    <list>
     <value>com/synnex/erp/kim/model/Kimpmduty.hbm.xml</value>
    </list>
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
     <prop key="hibernate.show_sql">false</prop>
     <prop key="hibernate.connection.release_mode">auto</prop>
     <prop key="hibernate.transaction.auto_close_session">true</prop>
     <prop key="show_sql">true</prop>
    </props>
   </property>
</bean>


数据源2包括的表:

<bean id="sessionFactoryLong" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource" ref="dataSourceCGLong" />
    <list>
    
     <value>com/synnex/erp/gms/rdb/model/RdbIvcStockTransRef.hbm.xml</value>
    </list>
   </property>
   <property name="hibernateProperties">
    <props>
&nb