Spring采用properties配置多个数据库
比如在一个项目中有这样的需求:上海和武汉采用不同的系统,每个系统都有自己的数据库,但是在上海的系统需要访问武汉的数据库,这就要在项目中配置两个数据源,下面是我给的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
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
<?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" /></property></bean><bean id="txProxyTemplate" abstract="true"/></property><property name="transactionAttributes"><props><prop key="*">PROPAGATION_REQUIRED</prop></props></property></bean><bean id="wuhanDataSource" name="code"><bean id="sessionFactory" /></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" /></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="blBuDriverService" parent="txProxyTemplate"> <property name="target"> <bean /></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 /></property> </bean> </property> </bean>
private BlBuDriverDAO blBuDriverDAO;private BlBuDriverDAO wuhanBlBuDriverDAO;