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

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

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

上修改之前的Spring-mybatis.xml代码:


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










       
       
       
       
       
   

    
   
       
       
       
       
       
   

    
   
       
       
           
               
               
           

       

   

    
   
   
       
       
   



   
       
   

    









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


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










       
       
       
       
       
   

    
   
       
       
       
       
       
   

    
   
       
       
           
               
               
           

       

   

    
   
   
       
       
   



   
       
   

   
   

          rollback-for="java.lang.Exception"/>
          rollback-for="java.lang.Exception" />
          rollback-for="java.lang.Exception" />
          rollback-for="java.lang.Exception" />








    


 






需要注意的是:

1:这里一定不能包括数据源文件;否则会出现:“Bean with name 'multipleDataSource' has been injected into other beans [txMgr..........”数据源重复加载错误

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

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

























你可能感兴趣的:(数据库)