SpringBoot Mybatis Mapper自动切换不同数据源

SpringBoot Mybatis Mapper自动切换不同数据源解决方案

场景:
同一boot服务使用多个数据源的场景并不少见,解决方案也有很多。

解决方案:
例如苞米豆的动态数据源。当时需要使用注解手动切换数据源。但此次介绍的是另一种解决方案,可以根据约定包名自动切换数据源。不喜码字,直接上代码。

数据源配置一

Configuration
@MapperScan(basePackages = "com.dahuatech.user.mapper.distributor", sqlSessionFactoryRef = "DistributorSqlSessionFactory")
public class DistributorDataSourceConfig {

    @RefreshScope
    @Bean(name="distributorDataSource",destroyMethod = "close",initMethod = "init")
    @ConfigurationProperties( "spring.datasource.distributor")
    public DruidDataSource dataSource(){
        return new DruidDataSource();
    }

    @Bean(name = "DistributorSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("distributorDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/dahuatech/user/mapper/distributor/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "DistributorTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("distributorDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "DistributorSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("DistributorSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

数据源配置二

@Configuration
@MapperScan(basePackages = "com.dahuatech.user.mapper.main",sqlSessionFactoryRef = "UserDBDataSqlSessionFactory")
public class UserDataSourceConfig {


    @RefreshScope
    @Bean(name="UserDBDataSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("securityDataSource")DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/dahuatech/user/mapper/main/*.xml"));

        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundWithCount","true");
        properties.setProperty("reasonable","false");
        properties.setProperty("supportMethodsArguments","true");
        properties.setProperty("params","pageNum=page;pageSize=size");
        interceptor.setProperties(properties);
        bean.setPlugins(new Interceptor[]{interceptor});
        return bean.getObject();
    }

    @Bean(name = "UserDBDataTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("securityDataSource")DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "UserDBDataSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("UserDBDataSqlSessionFactory")SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

目录结构组织SpringBoot Mybatis Mapper自动切换不同数据源_第1张图片

你可能感兴趣的:(解决方案,spring,boot,mybatis)