Spring采用properties配置多个数据库

来源:互联网 发布:孤岛惊魂3低配优化 编辑:程序博客网 时间:2024/03/29 07:28
在一个项目中有这样的需求:上海和武汉采用不同的系统,每个系统都有自己的数据库,但是在上海的系统需要访问武汉的数据库,这就要在项目中配置两个数据源,下面是我给的SSH采用properties配置数据源的方法。

1.要有两个properties文件
第一个文件是本地数据库文件dataResources.properties
ConnectString=jdbc:mysql://192.168.1.110:3306/tplis?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=trueUserID=rootPassword=rootJdbcDriverName=com.mysql.jdbc.Driver

第二个文件是武汉数据库文件wuhanDataResources.properties
wuhan.ConnectString=jdbc:mysql://localhost:3306/tplis?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=truewuhan.UserID=rootwuhan.Password=rootwuhan.JdbcDriverName=com.mysql.jdbc.Driver

2.配置dataSource
在applicationContext-database.xml文件中
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="propertyConfigurer"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">    <list>    <value>/WEB-INF/classes/net/bolue/resources/dataResources.properties</value>    <value>/WEB-INF/classes/net/bolue/resources/wuhanDataResources.properties</value>    </list>    </property>  </bean>  <bean id="dataSource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName">      <value>${JdbcDriverName}</value>    </property>    <property name="url">      <value>${ConnectString}</value>    </property>    <property name="username">      <value>${UserID}</value>    </property>    <property name="password">      <value>${Password}</value>    </property>  </bean> <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="txProxyTemplate" abstract="true"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean="transactionManager" /></property><property name="transactionAttributes"><props><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean><bean id="wuhanDataSource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName">      <value>${wuhan.JdbcDriverName}</value>    </property>    <property name="url">      <value>${wuhan.ConnectString}</value>    </property>    <property name="username">      <value>${wuhan.UserID}</value>    </property>    <property name="password">      <value>${wuhan.Password}</value>    </property>  </bean></beans>

3.配置sessionFactory
<bean id="sessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="dataSource"><ref bean="dataSource" /></property>    <property name="hibernateProperties">      <props>        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>        <prop key="hibernate.show_sql">true</prop>      </props>    </property>    <property name="mappingResources">      <list>        <!-- bu -->        <value>net/bolue/bu/vo/BlBuDriver.hbm.xml</value>        <value>net/bolue/basic/vo/BlBasicUser.hbm.xml</value>      </list>    </property>  </bean>

<bean id="wuhanSessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="dataSource"><ref bean="wuhanDataSource" /></property>    <property name="hibernateProperties">      <props>        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>        <prop key="hibernate.show_sql">true</prop>      </props>    </property>    <property name="mappingResources">      <list>        <!-- bu -->        <value>net/bolue/bu/vo/BlBuDriver.hbm.xml</value>        <value>net/bolue/basic/vo/BlBasicUser.hbm.xml</value>      </list>    </property>  </bean>

4.在DAO中注入sessionFactory
<bean id="blBuDriverService" parent="txProxyTemplate">    <property name="target">      <bean class="net.bolue.bu.service.impl.BlBuDriverServiceImpl">        <property name="blBuDriverDAO"><ref bean="blBuDriverDAO" /></property>        <property name="blBuTransDAO"><ref bean="blBuTransDAO" /></property>        <property name="wuhanBlBuDriverDAO"><ref bean="wuhanBlBuDriverDAO" /></property>      </bean>    </property>  </bean>

<bean id="blBuCustrelateService" parent="txProxyTemplate">    <property name="target">      <bean class="net.bolue.bu.service.impl.BlBuCustrelateServiceImpl">        <property name="blBuCustrelateDAO"><ref bean="blBuCustrelateDAO" /></property>      </bean>    </property>  </bean>

5.在service中使用两个不同的DAO
private BlBuDriverDAO blBuDriverDAO;private BlBuDriverDAO wuhanBlBuDriverDAO;
http://www.linuxso.com/architecture/8738.html