Spring Cloud + Mybatis 多数据源配置

一、数据源自动配置禁用

1、将spring boot自带的DataSourceAutoConfiguration禁掉,防止自动配置数据源。在@SpringBootApplication注解中添加exclude属性即可。

2、添加注解@EnableTransactionManagement 并创建两个事务管理器

@SpringBootApplication(exclude = {
		DataSourceAutoConfiguration.class
})
@EnableTransactionManagement
public class ManagerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ManagerApplication.class, args);
	}
	@Bean
	public PlatformTransactionManager testDBaTransactionManager(@Qualifier("testDBa") DataSource prodDataSource) {
		return new DataSourceTransactionManager(prodDataSource);
	}

	@Bean
	public PlatformTransactionManager testDBbTransactionManager(@Qualifier("testDBb") DataSource sitDataSource) {
		return new DataSourceTransactionManager(sitDataSource);
	}
}

二、serviceImpl层的@Transactional注解配置

在serviceImpl层的@Transactional注解中指定事务管理器。

  a、若service用到了两个操作不同数据源中mapper中的方法,则不指定事务管理器

  b、若service只用到了一个数据源的mapper,则需要指定事务管理器
@Service
@Transactional(value = "testDBaTransactionManager")
public class testaServiceImpl implements testaService {
    @Autowired
    private TestaMapper  testaMapper ;
}
@Service
@Transactional(value = "testDBbTransactionManager")
public class testbServiceImpl implements testbService {
    @Autowired
    private TestbMapper  testbMapper ;
}
@Service
@Transactional
public class testServiceImpl implements testService {
    @Autowired
    private TestaMapper  testaMapper ;
    @Autowired
    private TestbMapper  testbMapper ;
}

三、yml配置文件的修改

spring:
  datasource:
    testDBa:
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://xx.xx.xx.xx:xxxx/db?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: user
      password: pwd
    testDBb:
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://**.**.**.**:****/db?useUnicode=true&characterEncoding=utf8&autoReconnect=true
      username: user
      password: pwd

四、创建新的mapper文件夹

将需要连接新DB的mapper文件放进来

1、将需要连接新DataSource的xml文件放进来

2、src层也创建新的对应文件夹存放mapper.java文件

五、数据源的配置

DataSourceConfig.java

@Configuration
public class DataSourceConfig {

    @Bean(name = "testDBa")
    // application.properteis中对应属性的前缀
    @ConfigurationProperties(prefix = "spring.datasource.testDBa")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }




    @Bean(name = "testDBb")
    // application.properteis中对应属性的前缀
    @ConfigurationProperties(prefix = "spring.datasource.testDBb")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

}

六、Mybatis配置

接下来需要配置两个mybatis的SqlSessionFactory分别使用不同的数据源:

MybatisDbAConfig.java

@Configuration
@MapperScan(basePackages = {"testa.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {

    @Autowired
    @Qualifier("testDBa")
    private DataSource testDBa;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        // 使用testDBa数据源, 连接testDBa库
        factoryBean.setDataSource(testDBa);

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        // 使用注解中配置的Factory
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
        return template;
    }
}

MybatisDbBConfig.java

@Configuration
@MapperScan(basePackages = {"testb.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {

    @Autowired
    @Qualifier("testDBb")
    private DataSource testDBb;


    @Bean
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(configuration);
        // 使用testDBb数据源, 连接testDBb库
        factoryBean.setDataSource(testDBb);

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        // 使用注解中配置的Factory
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}
赞(52) 打赏
未经允许不得转载:优客志 » JAVA开发
分享到:

相关推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏