论坛首页 Java版 Spring

请问怎么在Spring+hibernate中配置访问多个数据库?

浏览 2808 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2005-09-06
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>
  <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
  <!-- Choose the dialect that matches your "dataSource" definition -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
      <value>com.microsoft.jdbc.sqlserver.SQLsServerDriver</value>
    </property>
    <property name="url">
      <value>jdbc:microsoft:sqlserver://locathost:1433;Database=test</value>
    </property>
    <property name="username">
      <value>sa</value>
    </property>
    <property name="password">
      <value>sa</value>
    </property>
    <property name="maxActive">
      <value>10</value>
    </property>
    <property name="maxIdle">
      <value>2</value>
    </property>
    <property name="maxWait">
      <value>120000</value>
    </property>
    <property name="defaultAutoCommit">
      <value>false</value>
    </property>
  </bean>
  <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
      <list>
        <value>hibernatebean/Userinfo.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">net.sf.hibernate.dialect.SQLServerDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.jdbc.fetch_size">50</prop>
        <prop key="hibernate.jdbc.batch_size">25</prop>
      </props>
    </property>
  </bean>
 
  <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
      <property name="sessionFactory"><ref local="mySessionFactory"/></property>
  </bean>
 
  <bean id="userInfoDAO" class="hibernate.UserInfoHibernateDAO">
    <property name="sessionFactory">
      <ref local="mySessionFactory"/>
    </property>
  </bean>
 
  bean id="userInfoService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="userInfoDAO"/></property>
    <property name="transactionAttributes">
     <props>
      <prop key="saveUserinfo">PROPAGATION_REQUIRED</prop>
     </props>
    </property>
  </bean>
</beans>
</beans>
   
最后更新时间:2005-09-06
哈哈,找找我写过的贴子,或者在我的BLOG当中有。
   
0 请登录后投票
最后更新时间:2005-09-14
在构造 sessionFactory 时,利用:

[code:1]<property name="dataSource"><ref local="dataSource"/></property>

<property name="dataSource"><ref local="dataSource1"/></property>

<property name="dataSource"><ref local="dataSource2"/></property> [/code:1]

这种方式可以实现多 sessionFactory。
   
0 请登录后投票
最后更新时间:2007-07-16
偶试过了,可以的,在spring的配置文件中可以定义n个DataSource, n个SessionFactory, n个dao,当然它们的名字要有区分的
   
0 请登录后投票
最后更新时间:2007-07-16
rosen 写道
在构造 sessionFactory 时,利用:

[code:1]<property name="dataSource"><ref local="dataSource"/></property>

<property name="dataSource"><ref local="dataSource1"/></property>

<property name="dataSource"><ref local="dataSource2"/></property> [/code:1]

这种方式可以实现多 sessionFactory。




你确定这样不会覆盖掉dataSource,dataSource1?
property 可不是List的哦
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐