mybatis多数据源实现

数据源配置:
spring:
  application:
    name: fin-web-service
  datasource:
    manage:
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/manage?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: root
      password: root
      connection-test-query: SELECT 1 FROM DUAL
    risk:
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/risk?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: root
      password: root
      connection-test-query: SELECT 1 FROM DUAL
@Configuration
@MapperScan(basePackages = "com.demo.fin.orm.manage.mapper", sqlSessionTemplateRef  = "mexcSqlSessionTemplate")
public class DataSource1Config {

   @Value("classpath*:mybatis/manage/*.xml")
   private Resource[] mapperLocations;

   @Bean(name = "mexcDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.manage")
   @Primary
   public DataSource mexcDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "mexcSqlSessionFactory")
   @Primary /*此处必须在主数据库的数据源配置上加上@Primary*/
   public SqlSessionFactory mSqlSessionFactory(@Qualifier("mDataSource") DataSource dataSource) throws Exception {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(dataSource);
      /*加载mybatis全局配置文件*/
      bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml"));
      /*加载所有的mapper.xml映射文件*/
      bean.setMapperLocations(mapperLocations);
      return bean.getObject();
   }

   @Bean(name = "mTransactionManager")
   @Primary
   public DataSourceTransactionManager mTransactionManager(@Qualifier("mDataSource") DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
   }

   @Bean(name = "mSqlSessionTemplate")
   @Primary
   public SqlSessionTemplate mSqlSessionTemplate(@Qualifier("mSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
      return new SqlSessionTemplate(sqlSessionFactory);
   }
}

 

@Configuration
@MapperScan(basePackages = "com.demo.fin.orm.risk.mapper", sqlSessionTemplateRef  = "finSqlSessionTemplate")
public class DataSource2Config {

   @Value("classpath*:mybatis/risk/*.xml")
   private Resource[] mapperLocations;

   @Bean(name = "rDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.risk")
   public DataSource rDataSource() {
      return DataSourceBuilder.create().build();
   }

   @Bean(name = "rSqlSessionFactory")
   public SqlSessionFactory rSqlSessionFactory(@Qualifier("rDataSource") DataSource dataSource) throws Exception {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(dataSource);
      /*加载mybatis全局配置文件*/
      Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml");
      bean.setConfigLocation(resource);
      /*加载所有的mapper.xml映射文件*/
      bean.setMapperLocations(mapperLocations);
      return bean.getObject();
   }

   @Bean(name = "rTransactionManager")
   public DataSourceTransactionManager rTransactionManager(@Qualifier("rDataSource") DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
   }

   @Bean(name = "rSqlSessionTemplate")
   public SqlSessionTemplate rSqlSessionTemplate(@Qualifier("rSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
      return new SqlSessionTemplate(sqlSessionFactory);
   }

}

 

启动类:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = { "xxx" })
public class ServerApplication {
public static void main(String[] args) {
   new SpringApplicationBuilder(ServerApplication.class).web(WebApplicationType.SERVLET).run(args);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值