笑看烟雨红尘 2022-07-22 14:58 采纳率: 77.8%
浏览 187
已结题

Spring Boot 整合 AbstractRoutingDataSource 切换数据源无效

Spring Boot 整合 AbstractRoutingDataSource 切换数据源无效
问题相关代码
@Aspect
@Component
public class DataSourceAspect {
    @Before(value = "@annotation(com.kiku.config.SwitchSource)")
    public void beforeOpt(JoinPoint joinPoint) {
        /*因为是对注解进行切面,所以这边无需做过多判定,直接获取注解的值,进行环绕,将数据源设置成远方,然后结束后,清楚当前线程数据源*/
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        SwitchSource switchSource = method.getAnnotation(SwitchSource.class);
        DataSourceHolder.setDataSource(switchSource.value());
    }

    @After(value = "@annotation(com.kiku.config.SwitchSource)")
    public void afterOpt() {
        DataSourceHolder.clearDataSource();
    }
}
@Target(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface SwitchSource {
    String DEFAULT_NAME = "testDataSource";

    String value() default DEFAULT_NAME;
}
public class DataSourceHolder {
    private static final ThreadLocal<String> dataSources = new InheritableThreadLocal<>();

    /**
     * 设置数据源
     */
    public static void setDataSource(String datasource) {
        dataSources.set(datasource);
    }

    /**
     * 获取数据源
     */
    public static String getDataSource() {
        return dataSources.get();
    }

    /**
     * 清除数据源
     */
    public static void clearDataSource() {
        dataSources.remove();
    }
}
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceHolder.getDataSource();
    }

    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }
}
@ConfigurationProperties(prefix = "spring.datasource")
public class MybatisConfig {
    private String url;
    private String username;
    private String password;
    private String driverClassName;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}
@Configuration
@EnableConfigurationProperties(MybatisConfig.class)
public class MyBatisMapperScannerConfig {
    @Autowired
    private MybatisConfig mybatisConfig;
    private static String MAPPER_LOCATOIN = "classpath*:/mapper/*.xml";
    
    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(mybatisConfig.getUrl());
        druidDataSource.setUsername(mybatisConfig.getUsername());
        druidDataSource.setDriverClassName(mybatisConfig.getDriverClassName());
        druidDataSource.setPassword(mybatisConfig.getPassword());
        return druidDataSource;
    }
    
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATOIN));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        // 自动将数据库中的下划线转换为驼峰格式
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setDefaultFetchSize(100);
        configuration.setDefaultStatementTimeout(30);
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean.getObject();
    }
}
@Configuration
public class TestMybatisConfig {
    private static String MAPPER_LOCATOIN = "classpath*:/mapper/*.xml";

    @Bean(name = "testDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test")
    public DataSource hisDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dynamicDataSource(@Qualifier("dataSource") DataSource dataSource, @Qualifier("testDataSource") DataSource hisDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("testDataSource", hisDataSource);
        return new DynamicDataSource(dataSource, targetDataSources);
    }
    
    @Bean("sqlSessionFactory2")
    @Primary
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATOIN));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        // 自动将数据库中的下划线转换为驼峰格式
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setDefaultFetchSize(100);
        configuration.setDefaultStatementTimeout(30);
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean.getObject();
    }
}
RestController
public class TestController {
    @Autowired
    private TestDao dao;
    
    @RequestMapping("test01")
    public String test01() {
        String name = dao.selectTest();
        return name;
    }
    
    @RequestMapping("test02")
    @SwitchSource
    public String test02() {
        String name = dao.selectTest();
        return name;
    }
}
<select id="selectTest" resultType="java.lang.String">
        select name from kiku
</select>
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&useSSL\=false&allowMultiQueries\=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.test.jdbc-url=jdbc:mysql://127.0.0.1:3306/testversionup?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&useSSL\=false&allowMultiQueries\=true&serverTimezone=Asia/Shanghai
spring.datasource.test.username=root
spring.datasource.test.password=root
spring.datasource.test.driver-class-name=com.mysql.jdbc.Driver
  • 写回答

2条回答 默认 最新

  • kse_music 2022-07-22 18:07
    关注

    在dynamicDataSource的bean上加上@Primary

      @Bean(name = "dynamicDataSource")
        @Primary
        public DynamicDataSource dynamicDataSource(@Qualifier("dataSource") DataSource dataSource, @Qualifier("testDataSource") DataSource hisDataSource) {
            Map<Object, Object> targetDataSources = new HashMap<>();
            targetDataSources.put("testDataSource", hisDataSource);
            return new DynamicDataSource(dataSource, targetDataSources);
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月1日
  • 已采纳回答 7月24日
  • 修改了问题 7月22日
  • 创建了问题 7月22日

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联