Spring+Mybatis之多数据源配置
来源:互联网 发布:idc数据机房 编辑:程序博客网 时间:2024/06/05 17:39
同一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 配置多数据源,切换数据源
- 新手程序员学习与进阶之路
- Andriod中网络编程实例
- 把函数当做返回值
- 【并发】java线程内存模型,线程、工作内存、主内存
- 《Objective-C编程全解》 读书笔记 五章 基于引用计数的内存管理
- Spring+Mybatis之多数据源配置
- 关于APP接口
- MySQL数据库设计规范
- hexo-theme-believe version2.0 说明文档
- DSP6778硬件仿真时,程序直接运行,无法进行断点设置仿真问题。
- 正则表达式(判断是否为数字)
- 关于PHP实现扫码登录
- 浅析char*和char c[]
- 加密算法