springboot 两个src_Springboot + Mybatis 多数据源配置

packagedemo.springboot.conf;importjava.util.HashMap;importjava.util.Map;importjavax.sql.DataSource;importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Primary;importorg.springframework.core.env.Environment;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importcom.alibaba.druid.pool.DruidDataSource;importdemo.springboot.enums.DatabaseType;/***

*@authordanny.yao

* springboot集成mybatis基本入口

* 1、创建数据源

* 2、创建SqlSessionFactory*/@Configuration

@MapperScan(basePackages="demo.springboot.mapper", sqlSessionFactoryRef="sessionFactory")public classMybatisConfig {

@Autowired

Environment environment;

@Value("${datasource.jdbc.driverClassName}")privateString dbDriver;

@Value("${datasource.jdbc.url}")privateString dbUrl;

@Value("${datasource.jdbc.username}")privateString dbUsername;

@Value("${datasource.jdbc.password}")privateString dbPassword;

@Value("${datasource.jdbc.url_stg}")privateString dbUrl_stg;

@Value("${datasource.jdbc.username_stg}")privateString dbUsername_stg;

@Value("${datasource.jdbc.password_stg}")privateString dbPassword_stg;/*** 创建 local环境 dataSource

*@throwsException*/@Bean(name="dataSourceFunctional")public DataSource dataSourceLocal() throwsException{

DruidDataSource dataSource= newDruidDataSource();

dataSource.setDriverClassName(dbDriver);

dataSource.setUrl(dbUrl);

dataSource.setUsername(dbUsername);

dataSource.setPassword(dbPassword);returndataSource;

}/*** 创建 回归环境 dataSource

*@throwsException*/@Bean(name="dataSourceRegression")public DataSource dataSourceStaging() throwsException{

DruidDataSource dataSource= newDruidDataSource();

dataSource.setDriverClassName(dbDriver);

dataSource.setUrl(dbUrl_stg);

dataSource.setUsername(dbUsername_stg);

dataSource.setPassword(dbPassword_stg);returndataSource;

}/*** 1、创建动态数据源

*@throwsException

* @Primary该注解表示在同一个接口有多个类可以注入的时候,默认选择哪个,而不是让@Autowired报错*/@Bean(name="dynamicDataSource")

@Primarypublic DynamicDataSource DataSource(@Qualifier("dataSourceFunctional") DataSource dataSourceFunctional,

@Qualifier("dataSourceRegression") DataSource dataSourceRegression){

Map targetDataSource = new HashMap<>();

targetDataSource.put(DatabaseType.function, dataSourceFunctional);

targetDataSource.put(DatabaseType.regression, dataSourceRegression);

DynamicDataSource dataSource= newDynamicDataSource();

dataSource.setTargetDataSources(targetDataSource);

dataSource.setDefaultTargetDataSource(dataSourceFunctional);returndataSource;

}/*** 2、根据数据源创建SqlSessionFactory

*@throwsException*/@Bean(name="sessionFactory")public SqlSessionFactory sessionFactory(@Qualifier("dynamicDataSource")DynamicDataSource dataSource) throwsException{

SqlSessionFactoryBean sessionFactoryBean= newSqlSessionFactoryBean();

sessionFactoryBean.setDataSource(dataSource);

PathMatchingResourcePatternResolver resolver= newPathMatchingResourcePatternResolver();

sessionFactoryBean.setMapperLocations(resolver.getResources(environment.getProperty("mybatis.mapperLocations"))); //*Mapper.xml位置

returnsessionFactoryBean.getObject();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值