在Spring中使用两个数据库

时间:2016-01-19 05:06:04

标签: spring multiple-databases

我想在Spring项目中使用两个数据库。 我正在使用spring-servlet.xml和applicationContext.xml来调用mySql数据库。 我怎么能这样做?

这是我的spring-servlet.xml。

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    FILE* p;
    int a;
    char str[10];

    p = popen("sed -n '$=' loop.c", "r");
    fgets(str, 10, p);                
    a = atoi(str);

    printf("%d", a);
}

                                              

    <?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/jdbc
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

<context:component-scan base-package="com.startinpoint.proj.PMS" />

<tx:annotation-driven transaction-manager="transactionManager" />
<!-- <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> -->

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://00.00.00.0.00/OMS_DEV"
    p:username="sipadmin" p:password="P@ger123" />



<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
       <list>
            <value>com.startinpoint.proj.PMS.ProjectModule.Project</value>
            <value>com.startinpoint.proj.PMS.PhaseModule.Phase</value>
            <value>com.startinpoint.proj.PMS.TaskModule.Task</value>
       </list>
    </property>

    <!-- 
    <property name="configLocation">
        <value>hibernate.cfg.xml</value>
    </property>
    -->
    <property name="hibernateProperties">
       <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
           <prop key="hibernate.show_sql">false</prop>
       </props>
    </property>
</bean>

请帮我解决这个问题。感谢

1 个答案:

答案 0 :(得分:0)

正如bigdestroy正确提到的那样,你应该使用多个引用不同数据源的sessionFactorytransactionManager bean。

有关详细信息,请参阅此blogpost