# dynamic_data_source **Repository Path**: wlOwn/dynamic_data_source ## Basic Information - **Project Name**: dynamic_data_source - **Description**: 动态数据源 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-11-03 - **Last Updated**: 2023-11-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### DynamicDataSourceConfig: 生成Mybatis相关bean,并生成自定义DynamicRoutingDataSource ### DynamicRoutingDataSource: 继承于AbstractRoutingDataSource,重写determineTargetDataSource,可动态创建实际的datasource对象,并使用map缓存datasource ### DataSourceAspect: 切面拦截页面请求,根据"参数"来决定访问实际的数据库信息,并把配置信息放入DynamicDataSourceContextHolder中的ThreadLocal ### DynamicDataSourceContextHolder: 负责管理threadLocal ### 注: 此情况可能导致,查询无法生效,异步查询,无法共享到threadLocal中的数据库配置信息。此时需要子线程共享父线程的上下文信息,可使用阿里的TransmittableThreadLocal ``` public class DemoController { @Resource private DemoService demoService; @RequestMapping("async") @ResponseBody public Object async() throws ExecutionException, InterruptedException { return CompletableFuture.runAsync(() -> demoService.getList()).get();; } } ```