spring配置多数据源

项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。例如在一个spring和hibernate的框架的项目中,我们在spring配置中往往是配置一个dataSource来连接数据库,然后绑定给sessionFactory,在dao层代码中再指定sessionFactory来进行数据库操作。

正如上图所示,每一块都是指定绑死的,如果是多个数据源,也只能是下图中那种方式。

可看出在Dao层代码中写死了两个SessionFactory,这样日后如果再多一个数据源,还要改代码添加一个SessionFactory,显然这并不符合开闭原则。

那么正确的做法应该是

代码如下:

1. applicationContext.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"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${db.conn.url}" />
    <property name="username" value="${db.conn.userName}" />
    <property name="password" value="${db.conn.password}" />
    <property name="initialSize" value="0" />
    <property name="maxActive" value="${db.conn.maxActive}" />
    <property name="maxIdle" value="${db.conn.maxIdle}" />
    <property name="minIdle" value="0" />
    <property name="maxWait" value="${db.conn.maxWait}" />
    <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    <property name="validationQuery" value="${validationQuery}" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="1800" />
    <property name="logAbandoned" value="true" />
    <!-- <property name="filters" value="stat" /> -->
    <property name="filters" value="mergeStat" />
  </bean>
  
<bean name="dataSourcePerson" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${person.db.conn.url}" />
    <property name="username" value="${person.db.conn.userName}" />
    <property name="password" value="${person.db.conn.password}" />
    <property name="initialSize" value="0" />
    <property name="maxActive" value="${db.conn.maxActive}" />
    <property name="maxIdle" value="${db.conn.maxIdle}" />
    <property name="minIdle" value="0" />
    <property name="maxWait" value="${db.conn.maxWait}" />
    <!-- <property name="poolPreparedStatements" value="true" /> 
<property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    <property name="validationQuery" value="${validationQuery}" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="1800" />
    <property name="logAbandoned" value="true" />
    <!-- <property name="filters" value="stat" /> -->
    <property name="filters" value="mergeStat" />
  </bean>
  
	<bean id="multipleDataSource" class="com.hnlat.common.util.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="dataSourcePerson" value-ref="dataSourcePerson"/>
            </map>
        </property>
    </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="multipleDataSource" />
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
      </props>
    </property>


    <property name="packagesToScan">
      <list>
        <value>com.hnlat.***.domain</value>
      </list>
    </property>
  </bean>


  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  	<property name="sessionFactory" ref="sessionFactory"/>
  </bean>


  <tx:annotation-driven transaction-manager="transactionManager" />


  <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" />
      <tx:method name="save*" />
      <tx:method name="update*" />
      <tx:method name="modify*" />
      <tx:method name="edit*" />
      <tx:method name="delete*" />
      <tx:method name="remove*" />
      <tx:method name="repair" />
      <tx:method name="deleteAndRepair" />

      <tx:method name="get*" propagation="SUPPORTS" />
      <tx:method name="find*" propagation="SUPPORTS" />
      <tx:method name="load*" propagation="SUPPORTS" />
      <tx:method name="search*" propagation="SUPPORTS" />
      <tx:method name="datagrid*" propagation="SUPPORTS" />

      <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
  </tx:advice>
    
     <aop:config>
    <aop:pointcut id="transactionPointcut" expression="execution(* com.hnlat.*.service..*Impl.*(..))" />
    <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
  </aop:config>

</beans>


2.MultipleDataSource.java

这个数据源类 有方法设置数据源的KEY就能切换数据源了

/**
 * 多数据源管理
 * @author admin
 *
 */
@Component
public class MultipleDataSource extends AbstractRoutingDataSource {
	private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

	public static void setDataSourceKey(String dataSource) {
		dataSourceKey.set(dataSource);
	}

	@Override
	protected Object determineCurrentLookupKey() {
		return dataSourceKey.get();
	}
}

3.MultipleDataSourceAspectAdvice.java

/**
 * 自动切换数据源
 * 
 * @author admin
 *
 */
@Component
@Aspect
public class MultipleDataSourceAspectAdvice {

	@Around("execution(* com.hnlat.*.service..*Impl.*(..))")
	public Object doAround(ProceedingJoinPoint jp) throws Throwable {
		// 如果是SSO的server就切换数据源
		if (jp.getTarget() instanceof ScholarService) {
			MultipleDataSource.setDataSourceKey("dataSourcePerson");
		} else {
			MultipleDataSource.setDataSourceKey("dataSource");
		}
		return jp.proceed();
	}

}

首先切面把所有的service都抓进来

然后判断根据不同的service 调用不同的 数据源

至此 每次调用不同的service都能切换到相对应的数据库了

所有要去dataSourcePerson数据源查询的都去实现ScholarService这个接口就行了

注:

<bean id="multipleDataSource" class="com.hnlat.common.util.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="dataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="dataSourcePerson" value-ref="dataSourcePerson"/>
            </map>
        </property>
    </bean>
这里在启动的时候是默认dataSource这个数据源,在hibernate使用注解实体与表关系的时候,会去默认的数据源查找表,也就是说如果你要使用hibernate注解dataSourcePerson这个数据源数据库里面的表时,启动是他却会去 dataSource里面找表,这样就会启动报错。这很尴尬!我不知道要如何解决这个问题,所以我只好dataSourcePerson的表dataSource里面穿件一个空表!





以上是第一种方法,还有第二种方法:

  1. applicationContext.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"
	xmlns:util="http://www.springframework.org/schema/util" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${db.conn.url}" />
    <property name="username" value="${db.conn.userName}" />
    <property name="password" value="${db.conn.password}" />
    <property name="initialSize" value="0" />
    <property name="maxActive" value="${db.conn.maxActive}" />
    <property name="maxIdle" value="${db.conn.maxIdle}" />
    <property name="minIdle" value="0" />
    <property name="maxWait" value="${db.conn.maxWait}" />
    <property name="validationQuery" value="${validationQuery}" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="1800" />
    <property name="logAbandoned" value="true" />
    <property name="filters" value="mergeStat" />
  </bean>
  
  
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="multipleDataSource" />
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
      </props>
    </property>

    <property name="packagesToScan">
      <list>
        <value>com.hnlat.***.domain</value>
      </list>
    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  	<property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <tx:annotation-driven transaction-manager="transactionManager" />

  <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" />
      <tx:method name="save*" />
      <tx:method name="update*" />
      <tx:method name="modify*" />
      <tx:method name="edit*" />
      <tx:method name="delete*" />
      <tx:method name="remove*" />
      <tx:method name="repair" />
      <tx:method name="deleteAndRepair" />
      <tx:method name="get*" propagation="SUPPORTS" />
      <tx:method name="find*" propagation="SUPPORTS" />
      <tx:method name="load*" propagation="SUPPORTS" />
      <tx:method name="search*" propagation="SUPPORTS" />
      <tx:method name="datagrid*" propagation="SUPPORTS" />
      <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
  </tx:advice>
 
     
    
    
    
    <bean name="dataSourcePerson" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${person.db.conn.url}" />
    <property name="username" value="${person.db.conn.userName}" />
    <property name="password" value="${person.db.conn.password}" />

    <property name="initialSize" value="0" />
    <property name="maxActive" value="${db.conn.maxActive}" />
    <property name="maxIdle" value="${db.conn.maxIdle}" />
    <property name="maxWait" value="${db.conn.maxWait}" />
    <property name="validationQuery" value="${validationQuery}" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="25200000" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="1800" />
    <property name="logAbandoned" value="true" />
    <property name="filters" value="mergeStat" />
  </bean>
  

    <bean id="personJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"  
        lazy-init="false" autowire="default" >  
      <property name="dataSource">  
            <ref bean="dataSourcePerson" />  
        </property>  
    </bean> 
    
     <aop:config>
    <aop:pointcut id="transactionPointcut" expression="execution(* com.hnlat.*.service..*Impl.*(..))" />
    <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
  </aop:config>

  
</beans>

2.personCommJdbcDao.java

@Repository
public class PersonCommJdbcDao{
	
	@Autowired
	@Qualifier("personJdbcTemplate")
	private JdbcTemplate template;
	
	
	public List<Map<String, Object>> queryList(String sql,Object...args){
		return template.queryForList(sql, args);
	}
}

这个使用的是spring的 JdbcTemplate方法,使用方法与jdbc类似,具体百度,我也是才了解!他没有集成hibernate!

其他方法还在了解

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值