关于Spring-Mybatis项目多数据源配置应用的心得

踩坑背景:起因是本人接手的项目需要同时连接MySQL与SQLserver数据库,也就是我们所说的使用多个数据源;刚开始我也是找度娘,然后通过学习大神的想法和笔记才得以配置完成,但是具体怎么写终究还得根据自己项目需求。跟着大神的脚步(踩在大神的肩膀上);发现配置Aop后系统并不能自动切换数据源;踩坑开始:

本人介绍之外的代码可参考这篇文章,写的还是挺好的:http://www.cnblogs.com/lzrabbit/p/3750803.html

上修改之前的Spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- spring-mybatis.xml -->
<!-- 配置数据源、连接池 -->
<!-- Mybatis 的 sqlSessionFactory -->
<!-- Mapper 接口自动扫描 -->


<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置多数据源 -->
<bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${sqlserver.driver}"/>
        <property name="url" value="${sqlserver.url}"/>
        <property name="username" value="${sqlserver.user}"/>
        <property name="password" value="${sqlserver.pwd}"/>
        <property name="maxActive" value="50"/>
    </bean>
    
    <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${mysql.driver}"/>
        <property name="url" value="${mysql.url}"/>
        <property name="username" value="${mysql.user}"/>
        <property name="password" value="${mysql.pwd}"/>
        <property name="maxActive" value="50"/>
    </bean>
    
    <bean id="multipleDataSource" class="com.lfhao.xbox.sqlmapper.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="mySqlDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="mySqlDataSource" value-ref="mySqlDataSource"/>
                <entry key="sqlServerDataSource" value-ref="sqlServerDataSource"/>
            </map>
        </property>
    </bean>
    
    <!-- sessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="multipleDataSource" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

<!-- mybatis.spring自动映射 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lfy.xbox.sqlmapper"/>
    </bean>
    <aop:aspectj-autoproxy/>
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务管理器必须注入 dataSource 对象 -->
<property name="dataSource" ref="multipleDataSource"/>
</bean>
<!-- 开启事务注解支持,可以使用注解管理事务 -->
<!-- 必须注入 事务管理器对象 -->
<tx:annotation-driven transaction-manager="txMgr" />
</beans>

然而运行发现在service层并不能自动切换数据源(也有可么可能是我没弄清楚的原因吧):然后又在网上看了很多文章,看到好多网友的评论也是在问这个问题怎么解决;然后我想到了在xml中直接配置数据源对具体的事务进行管理;这样不仅在service层实现数据源切换,而且在同一个方法内亦可切换,修改后如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- spring-mybatis.xml -->
<!-- 配置数据源、连接池 -->
<!-- Mybatis 的 sqlSessionFactory -->
<!-- Mapper 接口自动扫描 -->


<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置多数据源 -->
<bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${sqlserver.driver}"/>
        <property name="url" value="${sqlserver.url}"/>
        <property name="username" value="${sqlserver.user}"/>
        <property name="password" value="${sqlserver.pwd}"/>
        <property name="maxActive" value="50"/>
    </bean>
    
    <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${mysql.driver}"/>
        <property name="url" value="${mysql.url}"/>
        <property name="username" value="${mysql.user}"/>
        <property name="password" value="${mysql.pwd}"/>
        <property name="maxActive" value="50"/>
    </bean>
    
    <bean id="multipleDataSource" class="com.lfhao.xbox.sqlmapper.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="mySqlDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="mySqlDataSource" value-ref="mySqlDataSource"/>
                <entry key="sqlServerDataSource" value-ref="sqlServerDataSource"/>
            </map>
        </property>
    </bean>
    
    <!-- sessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="multipleDataSource" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

<!-- mybatis.spring自动映射 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lfhao.xbox.sqlmapper"/>
    </bean>
    <!-- 增强型事务管理 -->
    <tx:advice id="txAdvice" transaction-manager="txMgr">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" read-only="false" 
          rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>

<!-- 事物处理 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.lfhao.xbox.service..*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
    
<!--    <context:component-scan base-package="com.lfhao.xbox.sqlmapper"/>-->

  <!-- <aop:aspectj-autoproxy/> -->
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务管理器必须注入 dataSource 对象 -->
<property name="dataSource" ref="multipleDataSource"/>
</bean>
</beans>

需要注意的是:

1:<aop:pointcut id="pc" expression="execution(* com.lfhao.xbox.service..*(..))" />这里一定不能包括数据源文件;否则会出现:“Bean with name 'multipleDataSource' has been injected into other beans [txMgr..........”数据源重复加载错误

2:在Spring-aop.xml文件中<aop:aspectj-autoproxy proxy-target-class="false" />,要设置为false;解释如下:启动对@Aspectj的支持 true为cglib,false为jdk代理,为true的话,会导致拦截不了mybatis的mapper;

好了,大家还有什么疑问或者更好的方法,欢迎留言交流。

























  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dengrz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值