Spring+Mybatis之多数据源配置
来源:互联网 发布:动物照相软件 编辑:程序博客网 时间:2024/05/18 15:52
同一applicationContext.xml中:
1,配置多个数据源(采用连接池c3p0,直接jdbc连也行)
数据源1:dataSource1
<bean id = "dataSource1" class= "com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init = "default">
<property name="driverClass" value = ""/>
...
...
</bean>
数据源2:dataSource2
<bean id = "dataSource2" class= "com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init = "default">
<property name="driverClass" value = ""/>
...
...
</bean>
2, 配置mybatis数据会话管理(sqlSessionFactoryBean)
会话factory1:sqlSessionFactoryBean1
<bean id = "sqlSessionFactoryBean1" class = "mybatis.spring.SqlSessionFactoryBean">
<property name = "mapperLocations">
<list>
//不同factory,尽量保持其mapper的路径不同
<value>classpath:mapper/*Mapper.xml</value> //classpath指的是:src/main/resources(maven项目文件包结构哦)
</list>
</property>
<property name = "dataSource" ref = "dataSource1"></property>
</bean>
会话factory2:sqlSessionFactoryBean2
<bean id = "sqlSessionFactoryBean2" class = "mybatis.spring.SqlSessionFactoryBean">
<property name = "mapperLocations">
<list>
<value>classpath:mapper/*Mapper.xml</value>
</list>
</property>
<property name = "dataSource" ref = "dataSource2"></property>
</bean>
3, 配置Dao接口层(重点注意)
mybatis用mapperScannerConfigurer扫描相关的dao(或mapper)的interface的包,将其注入到spring进行管理;
dao层需要注入第2步配置的sessionFactory来获得session(session中加入事物等操作),跟sessionFactory中配置的*Mapper.xml进行一一匹配;
mapperScanner1:
<bean id="mapperScanner1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
//不同数据源的扫描dao层不要放在一起,不然数据源1扫描了数据源2的dao,那 去数据源1的mappe.xml中匹配时,会找不到相应方法,而报错!!!
</bean>
mapperScanner2:
<bean id="mapperScanner2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
</bean>
其他配置:事物配置(dataSourceTransactionManager),持久化模板(SqlSessionTemplate)
No unique bean of type [org.apache.ibatis.sessioin.SqlSessionFactory] is defined:expected single matching bean but found 2:[sqlSessionFactoryBean1,sqlSessionFactoryBean2]
解决问题:检查步骤3配置,是否有配置sqlSessionFactoryBeanName
常见错误2:
...Dao conflicts with existing, non-compatible bean definition of same name and class[org.mybatis.spring.mapper.MapperFactoryBean]
解决问题:检查步骤3配置,属性basePackage,两个数据源是否扫描了同一dao层包,若是,更改dao层包,不同数据源需扫描不同的dao层包。
参考:http://stackoverflow.com/questions/4746766/spring-and-mybatis-multiple-data-sources-setup
- Spring+Mybatis之多数据源配置
- Spring+Mybatis之多数据源配置
- Spring+mybatis配置动态数据源
- spring boot之多数据源
- mybatis 学习之多数据源整合
- Spring+SpringMVC+MyBatis数据源的配置
- spring + mybatis配置多个数据源
- spring+mybatis使用druid配置数据源
- Spring Boot+Mybatis动态数据源配置
- mybatis+spring动态数据源的配置
- mybatis 之spring 数据源配置详解
- spring注解方式配置mybatis数据源
- spring boot jpa之多数据源
- spring和mybatis集成动态切换数据源配置实现
- spring mvc + mybatis配置多个数据源问题。
- Spring+Mybatis+MySql双数据源配置(手动切换数据源)
- Spring整合Mybatis实现动态数据源切换教程配置
- spring+myBatis 配置多数据源,切换数据源
- [effectiv c++]条款2:尽量以const,enum,inline替换#define(static,const定义式)
- Qt包含工程集subdirs的使用
- Java检查异常和非检查异常区别
- 前端面试中常见的算法问题总结
- Cocos2dx在Android环境下自动包含所有文件
- Spring+Mybatis之多数据源配置
- 提供可用的BT5更新源,亲测可用
- 进程和线程的区别和联系
- WOJ1228-The Flower Garden
- 第十九节:下载文件
- 浅谈数据挖掘中的关联规则挖掘
- 适配器模式
- Java--mac下简单配置Tomcat
- SVN--Authorization failed错误