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

2.配置dataSource
在appli cationCont ext-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
在使用Spring框架进行Web开发时,我们经常会用到Druid连接池作为数据库连接池。有时我们需要同时连接多个数据库,那么如何在Druid中配置多个数据库呢? 首先,我们需要在application.properties或application.yml配置文件中添加多个数据库连接信息,如下所示: ```properties # 第一个数据库连接 spring.datasource.url=jdbc:mysql://localhost:3306/test1 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 第二个数据库连接 spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2 spring.datasource.test2.username=root spring.datasource.test2.password=123456 spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver ``` 其中,第一个数据库连接的配置信息是默认的,而第二个数据库连接的配置信息前缀为`test2`。这样,我们就成功配置了两个不同的数据库连接。 接下来,我们需要在Druid配置类中进行对应的配置。假设我们的Druid配置类名为`DruidConfig`,那么我们可以在该类中添加下面的两个`@Bean`注解: ```java @Configuration public class DruidConfig { // 第一个数据库连接 @Bean(name = "dataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DruidDataSourceBuilder.create().build(); } // 第二个数据库连接 @Bean(name = "test2DataSource") @ConfigurationProperties(prefix = "spring.datasource.test2") public DataSource test2DataSource() { return DruidDataSourceBuilder.create().build(); } } ``` 其中,`@Bean`注解的`name`属性分别对应两个连接的名字,而`@ConfigurationProperties`注解的`prefix`属性则分别对应配置文件中的前缀。 最后,在需要使用的地方,我们可以通过`@Qualifier`注解来指定需要使用哪个数据库连接: ```java @Service public class UserServiceImpl implements UserService { @Autowired @Qualifier("dataSource") private DataSource dataSource; @Autowired @Qualifier("test2DataSource") private DataSource test2DataSource; // ... } ``` 通过以上步骤,我们就成功地在Druid中配置多个数据库连接。这样,我们就可以方便地同时访问多个不同的数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值