Spring采用properties配置多个数据库

比如在一个项目中有这样的需求:上海和武汉采用不同的系统,每个系统都有自己的数据库,但是在上海的系统需要访问武汉的数据库,这就要在项目中配置两个数据源,下面是我给的SSH采用properties配置数据源的方法。

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

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

[b]2.配置dataSource[/b]
在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>

[b]3.配置sessionFactory[/b]

<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>

[b]4.在DAO中注入sessionFactory[/b]

<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>

[b]5.在service中使用两个不同的DAO[/b]

private BlBuDriverDAO blBuDriverDAO;
private BlBuDriverDAO wuhanBlBuDriverDAO;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值