关于Spring-Mybatis项目多数据源配置应用的心得
2017-09-08 14:10
399 查看
踩坑背景:起因是本人接手的项目需要同时连接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>
需要注
9ba4
意的是:
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;
好了,大家还有什么疑问或者更好的方法,欢迎留言交流。
本人介绍之外的代码可参考这篇文章,写的还是挺好的: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>
需要注
9ba4
意的是:
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;
好了,大家还有什么疑问或者更好的方法,欢迎留言交流。
相关文章推荐
- 关于maven+springMVC+mybatis搭建的web项目问题
- 关于Spring-mybatis项目报Could not resolve placeholder错误的解决方法
- 关于Spring Boot的学习心得。安装,集成Spring Boot 与 mybatis
- 关于在Vue项目中,运用electron打包成web桌面应用的心得浅见
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
- [SpringBoot]IDEA快速构建SpringBoot应用项目(二)整合Mybatis + Druid
- 关于部署(Spring+MyBatis)web项目到Linux服务器
- 关于IDEA中maven整合mybatis+spring+springMVC项目中mapper文件找不到问题的解决方案(org.apache.ibatis.binding.BindingExcepti
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- myeclipse+maven实现多模块项目struts+spring+mybatis
- JavaWeb---框架SpringMvc+Mybatis开发项目第三季(共三季)
- springboot+mybatis搭建可以运行的项目
- 关于spring+springMVC+myBatis的一些基础配置以及整合
- 基于spring boot项目的多数据源配置与分布式事务处理总结
- 分布式架构真正适用于大型互联网项目的架构! dubbo+zookeeper+springmvc+mybatis+shiro+redis
- 使用ReportViewer生成动态报告--项目应用心得(一)
- Springboot+mybatis搭建项目
- 创建Spring-Mybatis web项目,解决IDEA下找不到XXXmapper.xml文件的问题
- Spring+SpringMVC+Mybatis(SSM)框架搭建教程(二)-创建项目