springMvc+mybatis多数据源配置

1、配置多个数据源

这里使用DruidDataSource 作为数据库连接池

https://github.com/AlibabaTech/druid/wiki

新建 dataSource-main.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		 http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.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/jdbc 
         http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
         http://www.springframework.org/schema/task   
    	 http://www.springframework.org/schema/task/spring-task-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!--配置不同的数据库连接信息-->
    <bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
        <property name="maxActive"><value>${jdbc.maxActive}</value></property>
        <property name="maxWait"><value>${jdbc.maxWait}</value></property>
        <property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
        <property name="initialSize"><value>${jdbc.initSize}</value></property>
        <property name="removeAbandoned"><value>true</value></property>
        <property name="removeAbandonedTimeout"><value>180</value></property>
        <property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
    </bean>

	<bean id="mysqlResourceSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="byName">
		<property name="mapperLocations" value="classpath:sqlmap/*.xml" /><!--扫描指定的xml文件-->
		<property name="dataSource" ref="mysqlDataSource" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xxx.api.dao" /><!--扫描指定的dao-->
		<property name="sqlSessionFactoryBeanName" value="mysqlResourceSqlSessionFactory" />
	</bean>

	<bean id="mysqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="mysqlDataSource" />
	</bean>

	<tx:advice id="mysqlTxAdvice" transaction-manager="mysqlTransactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="batch*" propagation="REQUIRED" />
            <tx:method name="make*" propagation="REQUIRED" />
            <tx:method name="close*" propagation="REQUIRED" />
            <tx:method name="reset*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
	</tx:advice>
	<aop:aspectj-autoproxy />
	<aop:config>
		<aop:pointcut id="mysqlAllManagerMethod" expression="execution(* com.xxx.api.dao.*.*(..))" /><!--扫描指定的dao-->
		<aop:advisor advice-ref="mysqlTxAdvice" pointcut-ref="mysqlAllManagerMethod" />
	</aop:config>
</beans>

新建dataSource-branch.xml文件
<pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		 http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.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/jdbc 
         http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
         http://www.springframework.org/schema/task   
    	 http://www.springframework.org/schema/task/spring-task-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!--配置不同的数据库连接信息-->
    <bean id="branchDataSource" class="com.epeit.api.dto.MyBasicDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
        <property name="url"><value>${jj.jdbc.url}</value></property>
        <property name="username"><value>${jj.jdbc.username}</value></property>
        <property name="password"><value>${jj.jdbc.password}</value></property>
        <property name="maxActive"><value>${jdbc.maxActive}</value></property>
        <property name="maxWait"><value>${jdbc.maxWait}</value></property>
        <property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
        <property name="initialSize"><value>${jdbc.initSize}</value></property>
        <property name="removeAbandoned"><value>true</value></property>
        <property name="removeAbandonedTimeout"><value>180</value></property>
        <property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
    </bean>

	<bean id="branchSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="byName">
		<property name="mapperLocations" value="classpath:otherSqlMap/*.xml" /><!--配置扫描不同的xm-->
		<property name="dataSource" ref="branchDataSource" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xxx.api.otherDao" /><!--配置扫描不同的dao-->
		<property name="sqlSessionFactoryBeanName" value="branchSqlSessionFactory" />
	</bean>

	<bean id="branchTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="branchDataSource" />
	</bean>

	<tx:advice id="branchServerTxAdvice" transaction-manager="branchTransactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="batch*" propagation="REQUIRED" />
            <tx:method name="make*" propagation="REQUIRED" />
            <tx:method name="close*" propagation="REQUIRED" />
            <tx:method name="reset*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
	</tx:advice>
	<aop:aspectj-autoproxy />
	<aop:config>
		<aop:pointcut id="branchAllManagerMethod" expression="execution(* com.xxx.api.otherDao.*.*(..))" />
		<aop:advisor advice-ref="branchServerTxAdvice" pointcut-ref="branchAllManagerMethod" />
	</aop:config>
</beans>


 
</pre><pre>
2、在applicationContext.xml中引入这两个配置好的xml文件

 
<import resource="dataSource-main.xml"/>
<import resource="dataSource-branch.xml"/>


 
 

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值