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);
    }

}

目录结构组织在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值