读源码过程中读到yml中有设置主次数据源,次数据源没有启用,在看到DruidConfig中的配置时,没有搞明白,怎么让mybatis找到主数据源的,请教大神在哪里实现的。
附上数据源配置
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
dynamicDataSource这个名字mybatis能自动匹配到吗?我看系统基础功能没有指定数据源,怎么实现的使用master数据源
@Configuration
// 表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
// 指定要扫描的Mapper类的包的路径
@MapperScan("com.ruoyi.*.mapper")
public class ApplicationConfig
{
}
在扫描mapper时并没有指定refDataSource,是通过@primary 标记的首选datasource实现的吗?
我们使用mybaits时一般都是配置使用的数据源名称。现在用了AbstractRoutingDataSource实现切换数据源,那是否把动态数据源的名字和mybatis绑定呢?
DynamicDataSource 类实现了AbstractRoutingDataSource 接口,所以只要是获取DataSource找到DynamicDataSource 时就会调用determineCurrentLookupKey 方法获取数据源名称。如果没有找到就会使用默认的数据源。
这解决了我想要知道的关于为啥方法没有指定数据源也能找到的master的问题。
但是我仍是不明白,为什么mybatis就找到DynamicDataSource?尤其是并没有指定mybatis的数据源ref的情况下?
版权声明:本文为lizhiyuan_eagle原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。